i hope anybody can help me =)
I try to keep the example simple:
function myClass(){
//some Code
}
myClass.prototype.func1 = function(){
//some Code
}
myClass.prototype.func2 = function(){
document.getElementById("myEl").onclick = function(){
this.func1 //does not work, this is only the Element...
}
}
How does it works to call func1? I want to bind the onclick-event in func2.
First, you have to keep a reference to the current object. As you already noticed, inside the event handler,
thisrefers to the DOM element. You can do this withSecond, you have to actually call the function:
Complete example:
In the newer browsers you can also use
.bind()[MDN] to explicitly define whatthisshould refer to (see the MDN docs for a shim for other browsers):