[untitled]
7 comments
This.
heh.
heh.
I don't think you're doing this right. The first .then receives a function, but the second .then executes function.apply at construction time and receives only its result (null) as the callback.
Also, after understanding what's going on, existing solutions might be better than rolling your own: http://api.jquery.com/jQuery.proxy/ .
Also, after understanding what's going on, existing solutions might be better than rolling your own: http://api.jquery.com/jQuery.proxy/ .
In ES5 you have Function.prototype.bind that returns a new function with this bound to whatever you want. You can also pass extra default arguments. On older browsers you could use es5-shim for this functionality.
See https://developer.mozilla.org/en/JavaScript/Reference/Global...
See https://developer.mozilla.org/en/JavaScript/Reference/Global...
I do the following a lot (setTimeout as a relevant example):
setTimeout(
(function(scope){
return function(){scope.fire();};
})(this), 500
);
The short explanation is you return a function from an immediately-executing anonymous function that takes the current scope as a parameter.I have used this method many times before also, added it as an alternative implementation on the site.
You can also create a 'bind' that returns a function that when called applies the context you desire.
function bind(fn, selfObj) {
return function() {
return fn.apply(selfObj || window);
};
}
callback = bind(function(){ alert(this.hello); }, this);Solid example, I frequently find myself saying something to the effect of:
var this_autobot = this;
async_function = function(){
this_autobot.fire();
}Is "for noobs" really necessary?
var self = this; function(){ self.foo() }