Improving Function.prototype.bind

Axel Rauschmayer axel at rauschma.de
Thu Jan 5 16:32:51 PST 2012


> I do not believe (Axel) that the spec's Reference internal type is helpful as an externally-visible part of the language in any of this.

I believe you, I know nothing about how references are actually implemented. Python works like this:

    class Foo:
        def bar():
            pass

    $ f = Foo()
    $ f.bar
    <bound method Foo.bar of <__main__.Foo instance at 0x103e57950>>

It has always surprised me that JavaScript preserved `this` here:
    (new Date().toString)()   // (*)
But not here:
    (0, new Date().toString)()

Then I found out that spec-wise, (*) is very close to Python. But if implementations don’t have references then it’s also impossible to keep them around. One could produce a bound function whenever one evaluates a reference to a method, but I don’t know if it’s worth the trouble (performance and compatibility-wise).

If you picture a method invocation obj.foo() as a single construct (and not as first evaluating obj.foo and then applying the () operator) then things do make sense – evaluating obj.foo is a different thing and simply gives you back the function you put there. And, thankfully, with strict mode, things fail fast if you forget to bind() a method.

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com



More information about the es-discuss mailing list