javascript - Why does this return a reference to the global window object, even though 'this' is within another function -



javascript - Why does this return a reference to the global window object, even though 'this' is within another function -

if (typeof obj == 'undefined') { obj = {}; } obj.thing = new function () { if (typeof this.global == 'undefined') { this.global = (function () {return this;})(); } }

this.global assigned within of function. so, why homecoming reference window object?

console.log(this) > domwindow console.log(obj.thing.global) > domwindow console.log(window) > domwindow

i love understand better.

in es 3 , es 5 there this keyword associated every execution context (es 3) or lexical environment (es 5). value set according rules entering global or function code described in ecma-262 §10.4.

in code have:

this.global = (function () {return this;})();

where result of calling anonymous function assigned this.global. within anonymous function, value of this set according algorithm in §10.4.3.

since function called without setting value of this, , code not in strict mode, value of this set global object (which, in browser, window object) per step 2 of algorithm.

if code in strict mode, value of this within anonymous function undefined, value assigned this.global.

javascript

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -