Function.prototype.bind

David-Sarah Hopwood david.hopwood at industrial-designers.co.uk
Wed Sep 10 15:41:05 PDT 2008


Mark S. Miller wrote:
> On Tue, Sep 9, 2008 at 9:51 PM, Brendan Eich <brendan at mozilla.org> wrote:
>>> I have also assumed the existence of a "static" Function.apply which
>>> takes the function to be applied as an explicit first argument.
>> This was part of ES4 and it's in JS1.7+ in Firefox.
> 
> Since the function constructor is itself a kind of function, this
> overloading breaks Liskov substitutability. Suppose someone defines a
> logCall function:
> 
[corrected version]
> function logCall(thisVal, meth, args) {
>    log('applying ' + meth + ' to ' + args + ' with ' + thisVal);
>    return meth.apply(thisVal, args);
> }
> 
> doing a logged call to the Function constructor
> 
>     logCall(Function, null, ['foo', 'bar', 'foo + bar'])
> 
> will do the wrong thing for no good reason.

This can be fixed by either renaming Function.apply, or conditioning
on whether there are two or three arguments (the former is more elegant).
The important point is the need for a static 3-argument version of
'apply', not what it is called. I will suggest the name 'apply3' as
a starting point (for symmetry and convenience 'call3' should also be
provided).

Note that apply3 and call3 are not, AFAICS, reliably expressible in
terms of Function.prototype.apply and Function.prototype.call.

-- 
David-Sarah Hopwood


More information about the Es-discuss mailing list