Function.prototype.bind behaviour (call vs apply)

Brendan Eich brendan at mozilla.com
Fri Aug 21 20:20:28 PDT 2009


On Aug 21, 2009, at 8:11 PM, Juriy Zaytsev wrote:

> If `boundArgs` is an array of arguments to bind, then I think you  
> should be able to do this via something like:
>
> Function.prototype.bind.apply(targetFn, [thisArg].concat(boundArgs));
>
> // or maybe:
>
> boundArgs.unshift(thisArg);
> Function.prototype.bind.apply(targetFn, boundArgs);
>
> Which is not very elegant of course (first version also takes a  
> performance hit by creating unnecessary Array object).

But it gets the job done.

I think it's better to leave ES5 Function.prototype.bind as specified,  
based on Prototype's bind (and others like it), which take positional  
arguments to partially apply.

Then with spread ( http://wiki.ecmascript.org/doku.php? 
id=harmony:spread ) in Harmony, you can write  
foo.bind(thisArg, ...argsArray) instead of the above bind.apply  
mouthful.

/be


More information about the es-discuss mailing list