`this`: methods versus functions

Axel Rauschmayer axel at rauschma.de
Wed Nov 9 15:48:00 PST 2011


>> Or are there other plans to get it solved? I would still love to see that happen, it’s a remarkably subtle source of errors. Could functions adopt the block-lambda semantics of picking up the `this` of the surrounding scope when not invoked as methods? It seems like that could work in strict mode where no one expects `this` to have a value.
> 
> No, if you call such functions via object.method() references then this binds to object. You can't break such compatibility only at runtime, and only some of the time.

Got it. I’m assuming that’s a performance issue?

In principle, one can envision: Bind this lexically by default, override with receiver when called as a function.

var obj1 = {
    makeFunction: function() {
        return function () { return this; };
    }
}
var func = obj1. makeFunction();
console.log(func() === obj1); // true, lexical `this` by default

obj2 = {
    method: func
}
console.log(obj2.method() === obj2);  // true, dynamic this overrides lexical `this`


Sorry for bringing up this issue again, I’m still a bit hazy as to what the arguments against such a solution are, especially after having seen the semantics of block lambdas.

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

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



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111110/01773567/attachment.html>


More information about the es-discuss mailing list