Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I don't see the "exception to the exception", he is saying that if strict mode is on, "this" isn't the global object when saying foo(), it's undefined. And that's true.

It's possible to both have "use strict" and "this" be the global object (i.e. it's not true that it's never the global object), as simple as...

  "use strict"; // global scope
  var x = 1;

  (function() {
  "use strict";
  return this.x;
  }).call(this);


I am not sure that's quite what you want. Declaring x as a 'var' does not make it a property on the global object. Your example will return an 'undefined'.

I think this is what you want

  "use strict";
  this.x = 1;

  var ret = (function() {
    "use strict";
    return this.x;
  }).call(this);

  console.log(ret); //returns 1


I wrote "// global scope" beside the first statement to signify that that statement was run in global scope, same for the statement below it.

In global scope, the activation object is the global object, and so saying "var x = 1" will make the global object have a property x equal to 1.


Fair point, but I thought it was clear I was referring to the first "exception", that is, calling a function like "foo()". Your example, while interesting, doesn't apply.

I'll be more clear:

> When there is no ‘.’ the keyword ‘this’ is bound to the global object window.

False if the function has "use strict".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: