Function.prototype.bind

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


Brendan Eich wrote:
> On Sep 9, 2008, at 9:47 PM, David-Sarah Hopwood wrote:
> 
>> As an alternative to saying "the original bindings of..." in the spec,
>> we could provide a way to actually get the original bindings in
>> ECMAScript code. This is independently useful, e.g. for secure subset
>> run-times.
> 
> Have you read
> http://wiki.ecmascript.org/doku.php?id=strawman:lexical_scope yet?

I had seen a previous similar proposal, but I don't think it included
the fixed bindings for the primordials (Array etc.)

However, unlike what I proposed, 'strawman:lexical_scope' doesn't make
the bindings of the primordials that are visible when it is active
deeply immutable. I suppose that's difficult if you want the
prototypes to appear to be the same between code that is in such a
block and code that is not -- for instance, '"" instanceof String'
should work whether or not 'String' refers to a deeply immutable or
mutable constructor.

> The Harmony reference implementation work is very likely to cut back the
> old ES4 RI to ES3 and go forward from there. To avoid hijacking built-in
> behavior it needs something like the "use lexical scope" pragma.

So, is this a valid self-hosting specification of bind?

  (function() {
    use lexical scope;

    Function.prototype.bind = function(self, var_args) {
      const thisFunc = this;
      const leftArgs = Array.slice(arguments, 1);
      return function(var_args) {
        const args = leftArgs.concat(Array.slice(arguments, 0));
        return Function.apply(thisFunc, self, args);
      };
    };
  )();

Note that this isn't useful for ES3.1 unless 'use lexical scope' is
added to ES3.1 (or implied by ES3.1-strict).

>> 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.

Good. I think it should be in ES3.1.

-- 
David-Sarah Hopwood


More information about the Es-discuss mailing list