Volunteers needed: Function.prototype.bind() in ES3.1 pseudo-code

nanto_vi nanto at moon.email.ne.jp
Mon Sep 22 17:22:58 PDT 2008


I propose some modifications.

---------------------------------------------------------------

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.

The following steps are taken:

1. Let T be thisArg.
2. Let F be the this object.
3. If IsCallable(F) 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 function object that has no [[Construct]] method,
    no prototype property, a [[Call]] method that, when executed,
    performs the "Algorithm for the [[Call]] methods of functions
    returned from the bind method" and has the values T, F and A
    internally associated with it.
6. Call the [[Get]] method of F with the argument "length".
7. Call ToUint32(Result(6)).
8. Let N be the size of A.
9. Call the [[Put]] method of Result(5) with arguments "length"
    and max(Result(7) - N, 0).
10. Call the [[Put]] method of Result(5) with arguments "name"
     and "".
11. Call the [[Get]] method of F with the argument "parameters".
12. Let P be Result(11).
13. If P is not an Array, let P be a new array created as if by
     the expression new Array() where Array is the standard
     built-in constructor with that name.
14. Call the [[Put]] method of Result(5) with arguments
     "parameters" and P.slice(N) where slice is the standard
     built-in method described in 15.4.4.10.
15. Return Result(5).


Algorithm for the [[Call]] methods of functions returned from the
bind method.

When executed with zero or more arguments a function returned by
the original Function.prototype.bind method uses the values of T,
F 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.
2. Add all of the values in the arguments list provided to the
    call to this function to the end of the list Args, in the order
    they appear in that arguments list (see 11.2.4).
3. If T is null or undefined, use the this value provided by the
    caller; otherwise, call ToObject(T).
4. Invoke the [[Call]] method of F providing Result(3) as the this
    value and providing Args as the arguments values.
5. Return Result(4).

---------------------------------------------------------------

The changes are:

- The prototype property isn't needed as well as the [[Construct]]
   method.

- Set appropriate values for length, name, and parameters properties.

- The bound function should bind only arguments if thisArg is null
   or undefined.  This allows us to create partially applied methods
   such as:

Array.prototype.sortAsNumber =
   Array.prototype.sort.bind(null, function (a, b) { return a - b; });

-- 
nanto_vi (TOYAMA Nao)


More information about the Es-discuss mailing list