I'm now facing a js closure problem. In the code bellow, I'm trying to bind an event handler "getNum" to the button, the problem is that this handler is an attribute in an object, so I read it in a book it says I should put my handler in a closure function like bellow, the problem is that I don't really get it why "this" argument point to the correct object when I put the handler in an anonymous function; And why pass an "event" argument to that function, what does "event" argument referring to, is it the event object when I click the button?
this.num = 9;
var myObj = {
num : 81;
getNum : function(){return this.num;}
}
var btn = document.getElementById("my-btn");
// btn.addEventListener("click",myObj.getNum); this doesn't work cause "this" point to the btn object,which doesn't have num attribute.
btn.addEventListener("click",function(event){
myObj.getNum(event);
});//worked, but I don't know why.