mardi 4 août 2015

invoking lodash _.bind() in the same way as jQuery.proxy()?

As far as I can infer from the Lodash documentation, _.bind(func, thisArg, [partials]) simply returns a version of func where the scope of this is bound to whatever you pass in to thisArg. However, you then need to invoke that function seperately, ie:

var boundFunction = _.bind(aFunction, aThisTarget);
boundFunction();

Is there a way to use _.bind() or a different function in lodash in the same manner that one can use jQuery's .proxy(), in that it immediately invokes the function with the passed in scope?

I don't need/want to save the found function to a variable, as it's part of a callback. I just want to fire a function with a different scope (immediately). IE:

this.loadScripts(function() {
    // do something   
}, $.proxy(this, 'doSomethingElse', 'params') );

Aucun commentaire:

Enregistrer un commentaire