How can I in the easiest way access an instance variable inside a function?
function MyObject(){
//Instance variables
this.handler;
//Methods
this.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();
Note that I can set button.onclick = this.handler;. This is just an example. The main question is how I can access this.handler inside that function?
I can also define a new variable var handler = this.handlerto access this.handler. But If a change handlerwill this.handler also be changes?