This is actually a follow up question to my previous question, Access instance variable inside a function in javascript?.
I want to access an instance variable inside an anonymous function inside prototype method.
function MyObject(){
//Instance variables
this.handler;
}
//Methods
MyObject.prototype.enableHandler = function(){
var button = document.getElementById('button');
button.onclick = function(){
this.handler();//Is not working
}
}
var myObject = new MyObject();
myObject.handler = function(){
alert('Hello World!');
}
myObject.enableHandler();
JSFiddle http://jsfiddle.net/3cmvZ/
The example above was just to clarify how I can access an instance variable inside an anonymous function inside a prototype method. I already know that button.onclick = this.handler works.