In this example
var a = 1;
( function(x) {
function inner() {
alert(a);
alert(x);
alert(y);
}
var y = 3;
inner();
})(2);
When does function inner get created? during execution time or parsing time of outer anonymous function?
What is in the scope chain of function inner?
What is the difference between the execution context and scope chain of function inner?
Thanks for enlighting me in advance!
One is created each time the outer function is executed.
When you execute it, that execution gets a variable object (technically the spec calls this the “binding object of the variable environment”); that’s backed by the variable object created for the outer function call that created
inner; that’s backed by the global variable object. So:+------------------------------+ | global variable obj | +------------------------------+ ^ | +-----------------------------+ | variable obj for outer call | +-----------------------------+ ^ | +-----------------------------+ | variable obj for inner call | +-----------------------------+Every function call gets its own execution context. I’m not quite sure I understand what’s being asked here.
You can read up on all of this stuff (if you’re willing to wade through the treacle of turgid prose) in Section 10 of the spec, and in particular section 10.4.3: “Entering Function Code”.