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

JS -- The Scope Chain 作用域链的更多相关文章

  1. Scope Chain(作用域链)

    本章,我们讨论一下ECMAScript中的作用域链 , 开门见山. 什么是作用域链 i.ECMAScript是允许创建内部函数的,甚至能从父函数中返回这些函数.作用域链正是内部上下文中所有变量对象(及 ...

  2. [ JS 进阶 ] 闭包,作用域链,垃圾回收,内存泄露

    原网址:https://segmentfault.com/a/1190000002778015 1. 什么是闭包? 来看一些关于闭包的定义: 闭包是指有权访问另一个函数作用域中变量的函数 --< ...

  3. JS 执行环境与作用域链

    1.执行环境 JavaScript 代码都是在执行环境中被执行的.执行环境是一个概念,一种机制,用来完成JavaScript运行时在作用域.生命周期等方面的处理,它定义了变量或函数是否有权访问其他数据 ...

  4. 对JS闭包和函数作用域的问题的深入讨论,如何理解JS闭包和函数作用域链?

    首先先引用<JavaScript权威指南>里面的一句话来开始我的博客:函数的执行依赖于变量作用域,这个作用域是在函数定义时决定的,而不是函数调用时决定的. 因此,就出现了如下的几串代码: ...

  5. JS执行环境,作用域链及非块状作用域

    JS中的执行环境,顾名思义就是变量或函数所执行时的环境.在我的理解中,执行环境和作用域相差不大. 每个函数都有自己的执行环境,当执行流进入一个函数时,函数的环境就会被推入一个环境栈中.而在函数执行之后 ...

  6. 关于JS里的函数作用域链的总结

    在JavaScript中,函数的作用域链是一个很难理解的东西.这是因为JavaScript中函数的作用域链和其他语言比如C.C++中函数的作用域链相差甚远.本文详细解释了JavaScript中与函数的 ...

  7. JS进阶系列之作用域链

    在之前写的进阶系列里面,提到了执行上下文在创建阶段,要创建变量对象.确定作用域链还有确定this的指向,本次将重点讲解一下作用域链. JavaScript代码的执行过程 在讲解作用域链之前,首先了解一 ...

  8. js学习笔记之作用域链和闭包

    在学习闭包之前我们很有必要先了解什么是作用域链 一.作用域链 作用域链是保证对执行环境有权访问的所有变量和函数的有序访问. 这句话其实还是蛮抽象的,但是通过下面一个例子,我们就能清楚的了解到作用域链了 ...

  9. js考察this,作用域链和闭包

    在严格版中的默认的this不再是window,而是undefined. 先看两个例子 example one var num = 20; var obj = { num: 30, fn: (funct ...

随机推荐

  1. ORA-06575:程序包或函数处于无效状态

    今天一个朋友问我下面这段sql语句的问题,我发现了他竟然把程序员的编程思想带入了oracle,虽然是错误的,但也是很经典的错误啊. create or replace package p_view_p ...

  2. github如何添加ssh

    1.运行git Bash 输入如下命令: $ cd ~/.ssh $ ls 输入这2个命令 ,我们可以看到 id_rsa.pub 或 id_dsa.pub 这2个文件已经存在了,id_rsa 是私钥, ...

  3. 谈一谈EasyUI中TreeGrid的过滤功能

    写在最前面 这个星期一直在纠结easyui的treegrid的过滤功能,原因呢,自然是项目中一个莫名奇妙的需求. easyui虽说是后端程序员的前端框架,但是说句实话,除去api,让我直接写里面的节点 ...

  4. 关于时间对象Date()

    今天使用XCUI开发过程中发现另一个诡异的问题,就是年月日初始化之后默认时分秒的问题. 问题发生在重构交互日志页面的时候,原来的老页面是这样的: 进入了交互日志页面之后,默认会初始化时间为今天的凌晨到 ...

  5. js并行加载,顺序执行

    js并行加载,顺序执行 <script>运行脚本或加载外部文件时,会阻塞页面渲染,阻塞其他资源的加载.如果页面中需要加载多个js文件,在古老浏览器中性能会比较糟糕. 因此有了最原始的优化原 ...

  6. poj 1742 多重背包

    题意:给出n种面值的硬币, 和这些硬币每一种的数量, 要求求出能组成的钱数(小于等于m) 思路:一开始直接用多重背包套上去超时了,然后就没辙了,然后参考网上的,说只需要判断是否能取到就行了,并不需要记 ...

  7. 如何使用phpstudy本地搭建多站点(每个站点对应不同的端口)

    到http://phpstudy.net/a.php/208.html下载phpstudy 1.装完phpstudy后,(假设安装在D盘,安装后开启服务) 在D:\phpStudy\WWW\路径下创建 ...

  8. 作业2——英语学习APP的案例分析

    英语学习APP的案例分析 很多同学有误解,软件工程课是否就是理论课?或者是几个牛人拼命写代码,其他人打酱油的课?要不然就是学习一个程序语言,搞一个职业培训的课?都不对,软件工程有理论,有实践,更重要的 ...

  9. Swing-JPopupMenu弹出菜单用法-入门

    弹出菜单是GUI程序中非常常见的一种控件.它通常由鼠标右击事件触发,比如在windows系统桌面上右击时,会弹出一个包含“刷新”.“属性”等菜单的弹出菜单.Swing中的弹出菜单是JPopupMenu ...

  10. spring的配置文件和加载

    ①:创建applicationContext.xml配置文件放在src下 //applicationContext.xml代码 <?xml version="1.0" enc ...