I have some code.. ala
$.fn.someObj= function(){
this.opt = {
whatever : 'somevalue',
whateve2 : 'more values'
}
this.someMethod = function(){
//do something
$(someElem).bind('click',function(){
this.someOTHERMethod(); <----- ISSUE HERE
})
}
this.someOTHERMethod = function(){
// do more stuff
}
this.init = function(data){
$.extend(this.opt, data);
this.someMethod();
};
};
I can create a closure and fix the issue;
var that = this;
//code
that.someOTHERMethod(); <--- works
or if I remove the “this” from the method:
someOTHERMethod = function(){}
and just call it: someOTHERMethod(); < ---- works
But I am wondering if there is a more elegant way to get that outer func without a closure or ? Any ideas?
You don’t need a closure, you can just pass a reference to your function, and eliminate the wrapper anonymous function:
If you want the
thisvalue insidesomeOTHERMethodto besomeObj, then use$.proxytoo, as per zzzzBov’s answer.