Strict undefined this

Erik Arvidsson erik.arvidsson at gmail.com
Sat Aug 30 14:02:23 PDT 2008


On Fri, Aug 29, 2008 at 22:51, Brendan Eich <brendan at mozilla.org> wrote:
> #c wins by maximizing TC purity, utility, and safety.
>
> /be

d) Lexical scoping

I thought one of the points with changing *this* in strict mode was to
make it use lexical scoping where it "makes sense"?  I do agree that
preventing access to global is a noble goal and I hope we can still
achieve that.  My suggestion is to use lexical scoping and special
case *this* as *undefined* if lexical scoping would make *this* the
global object.

use strict;

var o1 = {
  f: function() {
    function g() {
      print(this);
    }
    g();
  },
  toString: function() {
    return 'o1';
  }
}

o1.f(); // prints o1

function f() {
  print(this);
}

f(); // prints undefined since this would be lexically resolved to [[Global]]
print(this); // prints [[Global]], since direct access.

var o2 = {
  toString: funciton() {
    return 'o2';
  }
};

o2.f = f;
o2.f(); // prints o2 since this is bound by property lookupl

It might be a bit tricky to spec when *this* as [[Global]] needs to be
changed to *undefined*

If we are changing *this* we need to realize that *this* in local
functions is a big issue for people learning JS and even experienced
programmers make that mistake once in a while.  Changing *this*
without fixing the most important issue peole have with *this* would
be a mistake.

-- 
erik


More information about the Es-discuss mailing list