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
usedvariable object
. precisely why variables or functions declared globally become properties ofglobal object
yet, these variable object
s not accessible. non-internal 1 global object, window
or this
(in global context).
the relevant section in specification #10: executable code , execution contexts.
Comments
Post a Comment