You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于[[scopes]]、执行上下文与闭包关联关系的技术咨询

闭包、执行上下文与[[Scopes]]:理清它们的关联

Great question—these concepts are deeply interconnected, so it’s totally normal to wonder how they fit together. Let’s unpack each piece and how they relate:

1. 先明确:执行上下文是什么?

Every time JavaScript executes a function (or runs global code), it creates an Execution Context—this is the environment that holds all the information needed to run that code:

  • A lexical environment (modern replacement for variable objects) containing local variables, function arguments, and function declarations.
  • The scope chain: a linked list of lexical environments that the current context can access (used to look up variables/functions).
  • The value of this for that execution.

When a function finishes running, its execution context is usually popped off the call stack and garbage-collected—unless something keeps a reference to its lexical environment. That’s where closures come in.

2. [[Scopes]]: 作用域链的可视化展示

Yes, the [[Scopes]] you see in Chrome DevTools is exactly the scope chain made visible. It’s the list of lexical environments that the function has access to, ordered from the innermost (the function’s own environment, if it’s been called) to the outermost (the global environment).

For example, if you have a nested function:

function outer() {
  const outerVar = "I'm outer";
  function inner() {
    const innerVar = "I'm inner";
    console.log(outerVar);
  }
  return inner;
}
const innerFunc = outer();

When you inspect innerFunc in DevTools, [[Scopes]] will show two entries: first the outer function’s lexical environment (with outerVar), then the global environment. This is the scope chain that inner uses to find variables when it runs.

3. 执行上下文与闭包的核心关联

Closures and execution contexts are directly linked through the lexical environment:

  • When you define a function inside another function, the inner function captures a reference to the outer function’s lexical environment as part of its scope chain.
  • Normally, when the outer function finishes running, its execution context is destroyed. But if you return the inner function (or pass it somewhere else), that reference to the outer’s lexical environment is kept alive.
  • When you later call the inner function, it creates a new execution context—but its scope chain still includes that preserved outer lexical environment. That’s the closure: the function can still access variables from the outer scope, even though the outer function’s execution context is long gone.

To tie this back to your example: the closures showing 5 and 3 are each holding onto different lexical environments from their respective outer function calls (or loop iterations). Each closure’s [[Scopes]] will reflect the specific lexical environment it captured, which is why they return different values.

快速总结你的疑问

  • Is [[Scopes]] just a representation of the scope chain? Yes, exactly—it’s DevTools’ way of showing you the scope chain that the function has access to.
  • Does it relate to execution contexts? Absolutely. Each entry in [[Scopes]] corresponds to the lexical environment from an execution context (either the function’s own, from an outer function, or global).
  • Is there a link between execution contexts and closures? Yes—closures exist because a function retains a reference to a lexical environment from an execution context that would otherwise have been destroyed. The closure’s scope chain keeps that environment alive, allowing the function to access its variables later.

内容的提问来源于stack exchange,提问作者Willem van der Veen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:55:42