`this`: methods versus functions

Brendan Eich brendan at mozilla.com
Wed Nov 9 16:23:54 PST 2011


On Nov 9, 2011, at 4:15 PM, Mark S. Miller wrote:

> On Wed, Nov 9, 2011 at 4:00 PM, Brendan Eich <brendan at mozilla.com> wrote:
> On Nov 9, 2011, at 3:48 PM, Axel Rauschmayer 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.

I remember longer, but could be misremembering. Anyway, your example here is great, thanks!

/be

> 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.
> 
> -- 
>     Cheers,
>     --MarkM

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111109/2748ae41/attachment-0001.html>


More information about the es-discuss mailing list