`this`: methods versus functions

Axel Rauschmayer axel at rauschma.de
Thu Nov 10 05:49:03 PST 2011


getX() is designed for a dynamic `this` (i.e. `this` is an instance of InnerPoint). My proposal would allow an external party to switch to lexical `this` (=== function Outer), simply by invoking it as a (non-method) function. I can see how this would be bad.


On Nov 10, 2011, at 11:56 , Andreas Rossberg wrote:

> On 10 November 2011 01:15, Mark S. Miller <erights at google.com> wrote:
>> On Wed, Nov 9, 2011 at 4:00 PM, Brendan Eich <brendan at mozilla.com> wrote:
>>> We talked about lexical this for functions long ago (Jan. 2008? at Google
>>> anyway) and IIRC Mark found a subtler flaw.
>> 
>> I think my original example was smaller and more elegant. But the following
>> is adequate to demonstrate the problem:
>>   function Outer(secret) {
>>     "use strict";
>>     this.v = secret;
>>     this.w = secret * 2;
>>     this.x = secret * 3;
>>     this.InnerPoint = function(x, y) {
>>       this.x = x;
>>       this.y = y;
>>     };
>>     this.InnerPoint.prototype = {
>>       getX: function() { return this.x; },
>>       getY: function() { return this.y; }
>>     };
>>   }
>> Alice does:
>>   var outer = new Outer(mySecret);
>>   var innerPoint = new outer.InnerPoint(3,5);
>>   bob(innerPoint); // passed innerPoint to Bob, who Alice does not trust.
>> Today, Bob, receiving innerPoint, has no way to obtain Alice's secret. Given
>> your proposal, Bob could do
>>   (1,innerPoint.getX)() / 3;
>> Today, if Bob does that, the getX call fails when it tries to evaluate
>> undefined.x.
> 
> I must be missing something here. Are you assuming that "new
> outer.InnerPoint(3,4)" would somehow receive `outer' as `this' instead
> of a fresh object? Why would that be the case with the "soft binding"
> described by Axel? Or is this only a counter example for "hard"
> lexical binding of `this'?
> 
> /Andreas
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> 

-- 
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/03bb5a0a/attachment-0001.html>


More information about the es-discuss mailing list