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

Garrett Smith dhtmlkitchen at gmail.com
Fri Sep 19 14:25:20 PDT 2008


On Fri, Sep 19, 2008 at 1:16 AM, Garrett Smith <dhtmlkitchen at gmail.com> wrote:
> On Thu, Sep 18, 2008 at 11:49 PM, Erik Arvidsson
> <erik.arvidsson at gmail.com> wrote:
>> On Thu, Sep 18, 2008 at 10:41, Garrett Smith <dhtmlkitchen at gmail.com> wrote:
>>> 2008/9/18 Mark S. Miller <erights at google.com>:
>
>
>>
>> There is really no point in exposing the two different cases in a
>> spec.  A spec should describe the semantics, not the implementation.
>>
>
> OK.
>
> Another version:
> ================================================
> Function.prototype.bind( context, [ preArg1 [, preArg1 [,...]]])
>
> 1. if this object does not have a [[Call]] property, throw a
> TypeError
> 2. let FUN = this
> 3. let PREARGS = a copy of the argument list provided by the caller,
> starting from position 1.
> 4. Call [[Get]] method of PREARGS with argument "length".
> 5. let RETFUN = a new Function object.
> 6. let POSTARGS = a copy of FUN's argument list, starting from
> position Result(4).
> 7. Set RETFUN's FormalParameterList to POSTARGS.
> 8. let SCOPE = a new Object.
> 9. For each property in PREARGS, create a Property on SCOPE with a
> corresponding name and value.
> 10. Set RETFUN's scope chain to SCOPE
> 11. Set the Base object for RETFUN to context.
<ins>
12. set RETFUN's FunctionBody to an equivalent of FUN's FunctionBody.
13. Return RETFUN.
</ins>
>
> The length property of the bind method is 1.
> ================================================
>
> Shorter. A little easier to read in that it has no branch.
>
> What about step 11?
> 11. Set the Base object for RETFUN to context.
>
> The problem I see with that is that given:
>
> var n = f.bind(o);
> n.call(p);
>
> n.call will set the execution context to p. That seems wrong. n should
> always execute in context of o. Shouldn't it?
>
> Interesting Side effect: The returned function's length is FUN.length
> - PREARGS.length;
>
> Thoughts?
>
> Garrett
>
>>> ================================================
>>>
>>> ?
>>>
>>> Garrett
>>>
>>>> --
>>>>    Cheers,
>>>>    --MarkM
>>>>
>>>> _______________________________________________
>>>> Es-discuss mailing list
>>>> Es-discuss at mozilla.org
>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>
>>>>
>>> _______________________________________________
>>> Es-discuss mailing list
>>> Es-discuss at mozilla.org
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
>>
>>
>>
>> --
>> erik
>>
>


More information about the Es-discuss mailing list