function factory() { var name = 'laruence'; var intro = function(){ alert('I am ' + name); } return intro; } function app(para){ var name = para; var func = factory(); func(); } app('eve');
contentStack = {
globalContext
}
globalContext = {
globalariableObject : [factory,app],
globalScopeChain : globalariableObject
}
app.[[Scope]] = globalScopeChain;
----------------------------------------------
contentStack = {
appContext,
globalContext
}
appContext = {
appariableObject : [arguments,para,name,func,factory,app],
appScopeChain : [appariableObject, app.[[Scope]]]
}
factory.[[Scope]] = appScopeChain;
-----------------------------------------------
contentStack = {
factoryContext,
appContext,
globalContext
}
factoryContext = {
factoryariableObject : [arguments, name, intro,factory],
factoryScopeChain : [factoryariableObject, factoryScope]
}
intro.[[Scope]] = factoryScopeChain;
------------------------------------------------
contentStack = {
introContext,
factoryContext,
appContext,
globalContext
}
introContext = {
introariableObject = [arguments,name,intro],
introScopeChain = [introariableObject, intro.[[Scope]]]
}
因此最终的执行结果是intro可以访问到的name,也即laruence。