Draft of Function.prototype.bind.

Mark S. Miller erights at google.com
Fri Oct 31 19:55:48 PDT 2008


First, I'd like to thank Richard Cornford, cc'ed, who provided the
earlier draft of Function.prototype.bind that grew into this one.
Thanks!

Below, I modified Richard's draft according to our agreements in
Redmond. But I'm not happy with its complexity. If we instead specify
that we test in step 3 whether the [[Class]] of G is "Function", the
rest of bind() becomes much easier to specify. Should we? The only
functionality lost is the ability to use bind() as a generic applied
to callable non-functions. Do we care whether the built-in bind() can
handle these?


15.3.4.5    Function.prototype.bind (thisArg [, arg1 [, arg2, …]])
The bind method takes one or more arguments, thisArg and (optionally)
arg1, arg2, etc, and returns a new function object by performing the
following steps:
1.    Let T be thisArg.
2.    Let G be the this object.
3.    If IsCallable(G) is false, throw a TypeError exception.
4.    Let A be a new (possibly empty) internal list of all of the
argument values provided after thisArg (arg1, arg2 etc), in order.
5.    Create a new native ECMAScript object and let F be that object.
6.    Set the [[Class]] property of F to "Function".
7.    Set the [[Prototype]] property of F to the original Function
prototype object as specified in 15.3.3.1.
8.    Set the [[Call]] property of F as described in 15.3.4.5.1.
9.    Set the [[Construct]] property of F as described in 15.3.4.5.2.
10.    The [[Scope]] property of F has no observable effect, and so
can be ignored.
11.    If the [[Class]] property of G is "Function", then
        a.    Get the length property of G.
        b.    Let L be Result(11a) minus the length of A.
        c.    Set the length property of F to either 0 or L, whichever
is larger.
12.    Else set the length property of F to 0.
13.    The length property of F is given attributes as specified in 15.3.5.1.
14.    Set the [[Extensible]] property of F to true.
15.    If the [[Class]] property of G is "Function", then
        a.    Get the prototype property of G
        b.    Set the prototype property of F to Result(15a).
16.    Else
        a.    Create a new object as would be constructed by the
expression new Object()where Object is the standard built-in
constructor with that name.
        b.    Set the constructor property of Result(16a) to F. This
property has attributes { [[Writable]]: true, [[Enumerable]]: false,
[[Configurable]]: true }.
        c.    Set the prototype property of F to Result(16a).
17.    The prototype property of F is given attributes as specified in 15.3.5.2.
18.    Return F.

15.3.4.5.1     [[Call]]
Algorithm for the [[Call]] method of a function F returned from the bind method:
When executed with zero or more arguments, F uses the values of T, G
and A that were associated with it at its creation, and the following
steps are taken:
1.    Let Args be a new internal list containing the same values as
the list A in the same order followed by the argument list passed to F
in the same order.
2.    Invoke the [[Call]] method of F providing T as the this value
and providing Args as the arguments.
3.    Return Result(3).

15.3.4.5.2        [[Construct]]
Algorithm for the [[Construct]] method of a function F returned from
the bind method:
When executed with zero or more arguments, F uses the values of G and
A that were associated with it at its creation, and the following
steps are taken:
1.    If G has no [[Construct]] method, a TypeError exception is thrown.
2.    Let Args be a new internal list containing the same values as
the list A in the same order followed by the argument list passed to F
in the same order.
3.    Invoke the [[Construct]] method of F providing undefined as the
this value and providing Args as the arguments.
4.    Return Result(3).


--
   Cheers,
   --MarkM


More information about the Es-discuss mailing list