Hacker News new | ask | show | jobs
by raju 4913 days ago
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
1 comments

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.