我创建了一个非常简单的示例,它将作为对我的问题的解释。 该代码是伪代码,这里是:
var scopePosition = "global";
function outer() {
var scopePosition = "outer";
}
// debugger; We reached this line to inspect Execution Contexts and Function Objects
outer();
// Execution Contexts
var GlobalExecutionContext = {
variableObject: {
scopePosition: "global",
outer: "pointer to function object called `outer`"
},
scopeChain: [GlobalExecutionContext.variableObject],
this: {}
};
//Function Objects
var outerFunctionObject = {
[[Scope]]: "pointer to GlobalExecutionContext or GlobalExecutionContext.variableObject or
GlobalExecutionContext.scopeChain ???"
};
问题本身是何时创建函数的[[Scope]]属性确切指向何处?是它创建的执行上下文还是执行上下文变量对象或执行上下文作用域链?
我最好的猜测是GlobalExecutionContext.scopeChain,如果在外部内部有更多嵌套的函数并且重复该过程,则导致范围链接不会丢失。