javascript - Why do variables in the global scope get assigned to the window object? -


var foo = 'bar'; console.log(window.foo); // bar 

seems variables assigned properties this, inside anonymous functions, this refers parent scope, doesn't assign variables parent scope.

function() {     var foo = 'bar'; }();  window.foo; // undefined 

what object variables assigned in non-global scopes?

to cite http://perfectionkills.com/understanding-delete/#execution_context:

every execution context has so-called variable object associated it. execution context, variable object abstract entity, mechanism describe variable instantiation. now, interesing part variables , functions declared in source text added properties of variable object.

when control enters execution context global code, global object used variable object. precisely why variables or functions declared globally become properties of global object

yet, these variable objects not accessible. non-internal 1 global object, window or this (in global context).

the relevant section in specification #10: executable code , execution contexts.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -