博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JS -- The Scope Chain 作用域链
阅读量:4951 次
发布时间:2019-06-11

本文共 3373 字,大约阅读时间需要 11 分钟。

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语句和闭包很有帮助。

转载于:https://www.cnblogs.com/aimkk/p/7611402.html

你可能感兴趣的文章
mariadb BINLOG_FORMAT = STATEMENT 异常
查看>>
如何监视性能和分析等待事件
查看>>
C3P0 WARN: Establishing SSL connection without server's identity verification is not recommended
查看>>
iPhone在日本最牛,在中国输得最慘
查看>>
动态方法决议 和 消息转发
查看>>
关于UI资源获取资源的好的网站
查看>>
WPF自定义搜索框代码分享
查看>>
js 基础拓展
查看>>
Windows下常用测试命令
查看>>
SpringBoot访问html访问不了的问题
查看>>
{width=200px;height=300px;overflow:hidden}
查看>>
C#生成随机数
查看>>
CSS基础学习 20.CSS媒体查询
查看>>
2019春季第十一周作业
查看>>
洛谷P4591 [TJOI2018]碱基序列 【KMP + dp】
查看>>
iOS CoreData介绍和使用(以及一些注意事项)
查看>>
OS笔记047代理传值和block传值
查看>>
Android应用程序与SurfaceFlinger服务的连接过程分析
查看>>
coco2dx服务器简单例子
查看>>
Java回顾之多线程
查看>>