JS -- The Scope Chain 作用域链
The Scope Chain
JavaScript is a lexically scoped language: the scope of a variable can be thought of as the set of source code lines for which the variable is defined.
JS 是一个词法作用域语言,可以理解为变量的作用域就是变量所定义的源代码源代码范围处。
Global variables are defined throughout the program. Local variables are defined throughout the function in which they are declared, and also within any functions
全局变量作用域就是贯穿整个程序,局部变量作用域,就是在当前函数内部以及嵌套在当前函数内的嵌套函数中。
nested within that function.If we think of local variables as properties of some kind of implementation-defined object, then there is another way to think about variable
但是如果把局部变量当成对象(也就是:call object)的属性来理解的时候,那么对局部变量作用域就有了新的认识。
scope. Every chunk of JavaScript code (global code or functions) has a scope chain associated with it. This scope chain is a list or chain of objects that defines the
每个JS代码块(全局代码块或者函数块)都有关联的作用域链。这个作用域链就是一个对象链。
variables that are “in scope” for that code. When JavaScript needs to look up the value of a variable x (a process called variable resolution), it starts by looking at the
当JS需要查找 变量 x 的值,先找对象链的第一个对象,
first object in the chain. If that object has a property named x, the value of that property is used. If the first object does not have a property named x, JavaScript
如果这个对象有 x 属性,其属性值就会被使用。如果第一个对象没有这个属性 x,
continues the search with the next object in the chain. If the second object does not have a property named x, the search moves on to the next object, and so on. If x
JS 继续在对象链中找下面一个对象,如果第二个还是没找到属性 x,那么就在继续找下一个,如此类推。
is not a property of any of the objects in the scope chain, then x is not in scope for that code, and a ReferenceError occurs.
如果整个对象链都没找到,那么就会引发一个 ReferenceError 错误。
In top-level JavaScript code (i.e., code not contained within any function definitions), the scope chain consists of a single object, the global object.
在JS最顶层代码中(意思就是,代码不包含着任何 函数定义内),那么作用域链就只有一个对象,即:global 对象。
In a non-nested function, the scope chain consists of two objects. The first is the object that defines the function’s parameters and local variables, and the second is
在非嵌套函数中,作用域链包含两个对象。第一个包含 当前函数的 参数和局部变量,第二个就是全局对象。
the global object. In a nested function, the scope chain has three or more objects. It is important to understand how this chain of objects is created. When a function
在嵌套函数中,作用域链就会至少包含三个对象。那么这个作用域链中的对象是怎么创建的,当一个函数被定义以后,那么其保存的作用域链就起作用了,
is defined, it stores the scope chain then in effect. When that function is invoked, it creates a new object to store its local variables, and adds that new object to the
当函数被调用,那么就会创建一个对象来保存局部变量,并把这个对象添加到函数的作用域链上,
stored scope chain to create a new, longer, chain that represents the scope for that function invocation. This becomes more interesting for nested functions because
那么当前函数调用的作用域链就会形成了。如果函数内部有嵌套函数,
each time the outer function is called, the inner function is defined again. Since the scope chain differs on each invocation of the outer function, the inner function will
那么每次外部函数被调用,其内部函数都会被重新定义。由于每次函数调用,作用域链都会不一样,所以内部函数
be subtly different each time it is defined—the code of the inner function will be identical on each invocation of the outer function, but the scope chain associated with
每次定义所管理的作用域链也不一样。
that code will be different.
This notion of a scope chain is helpful for understanding the with statement and is crucial for understanding closures .
理解作用域链对理解 with语句和闭包很有帮助。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?