On Aug 28, 2008, at 12:14 PM, Dave Herman wrote:

> I interpreted Erik's point to be that the binding of `this' is not
> lexically scoped, so it would be useful to have a lexically scoped
> variable initially bound to the global object. IOW, if I write:
>
>      this.print("blah blah blah")
>
> and then I refactor the code to say:
>
>      (function() {
>          this.print("blah blah blah")
>      })()
>
> it breaks.

It happens not to break, because ES3 requires a null |this| for such  
calls, where the null is later replaced by the global object.

obj = {
     method: function () { this.print("blah blah blah"); },
     print:  function () { print("not the print you want"); }
};
obj.method();

would break, though.

We've been trying to fix the ES3 null->global rule for a while. Any  
change is an incompatible change, but the current rule leads to  
unintended global mutation and capture bugs.


> By contrast if I have a standard library binding `global'
> that's bound to the same thing as `this' at the top level, then I  
> can write:
>
>      global.print("blah blah blah")
>
> and the same refactoring:
>
>      (function() {
>           global.print("blah blah blah")
>      })()
>
> continues to work the same. But there's no need for a special  
> keyword or
> anything like that.

Indeed, Doug Crockford proposed at the January TC39 meeting this year  
to make 'this' act like a lexically bound variable, with the only  
magic to it applying to the case of obj.method() call expressions  
(and variatons, obj[name] where name = 'method'), where 'this' would  
be overridden -- if you will, a shadowing 'this' would be bound to obj.

I liked Doug's proposal quite a bit. I do not see anything like it in  
ES3.1, but I'd like to see it in Harmony.

/be
_______________________________________________
Es-discuss mailing list
Es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to