I was messing around in Javascript to be able to access sibling functions in an object. Let's say you have the following code:
function MyObject(){
this.click = function(){
// Here I want to call myFunction
}
this.myFunction = function(){
}
whatever.addEventListener('click', this.click, false);
}
I used an ugly hack to access myFunction. I passed the object instance's variable name to the object as a parameter and used it to access the function using the eval function. Eew!
There is a far more simple way to do this. Create a variable in your object and assign this to it:
function MyObject(){
var obj = this;
this.click = function(){
obj.myFunction();
}
this.myFunction = function(){
}
whatever.addEventListener('click', this.click, false);
}
No comments:
Post a Comment