JavaScript is a lexically scoped language: the scope of variable can be thought of as the set of source code lines for which the variable is defined. 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 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 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 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 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 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.

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. 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 identical on each invocation of the outer function, but the scope chain associated with that code will be different.

The Scope Chain的更多相关文章

  1. JavaScript的语法要点 2 - Scope Chain

    前文所述,JavaScript是基于词法作用域(lexically scoped)的,所以标识符被固定在它们被定义的作用域而不是语法上或是其被调用时的作用域.即全局变量的作用域是整个程序,局部变量的作 ...

  2. JS -- The Scope Chain 作用域链

    The Scope Chain JavaScript is a lexically scoped language: the scope of a variable can be thought of ...

  3. 你不知道的JavaScript--Item20 作用域与作用域链(scope chain)

    作用域是JavaScript最重要的概念之一,想要学好JavaScript就需要理解JavaScript作用域和作用域链的工作原理.今天这篇文章对JavaScript作用域和作用域链作简单的介绍,希望 ...

  4. 深入理解JavaScript系列(14):作用域链(Scope Chain)

    前言 在第12章关于变量对象的描述中,我们已经知道一个执行上下文 的数据(变量.函数声明和函数的形参)作为属性存储在变量对象中. 同时我们也知道变量对象在每次进入上下文时创建,并填入初始值,值的更新出 ...

  5. (翻译) How variables are allocated memory in Javascript? | scope chain | lexicial scope

    总结: 阅读下面文章需要15分钟 提问者的问题是JavaScript中内存是怎么分配的,在介绍的过程作者涉及计到了JS中 Scope Chain和调用函数call生成lexicial environm ...

  6. Scope Chain(作用域链)

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

  7. javaScript高级教程(二)Scope Chain & Closure Example

    <!DOCTYPE html> <html> <head> <meta charset=gb2312 /> <title>js</ti ...

  8. JavaScript - 运行机制,作用域,作用域链(Scope chain)

    参考 https://www.jianshu.com/p/3b5f0cb59344 https://jingyan.baidu.com/article/4f34706e18745be386b56d46 ...

  9. Javascript 的执行环境(execution context)和作用域(scope)及垃圾回收

    执行环境有全局执行环境和函数执行环境之分,每次进入一个新执行环境,都会创建一个搜索变量和函数的作用域链.函数的局部环境不仅有权访问函数作用于中的变量,而且可以访问其外部环境,直到全局环境.全局执行环境 ...

随机推荐

  1. 利用spring的ApplicationListener实现springmvc容器的初始化加载

    1.我们在使用springmvc进行配置的时候一般初始化都是在web.xml里面进行的,但是自己在使用的时候经常会测试一些数据,这样就只有加载spring-mvc.xml的配置文件来实现.为了更方便的 ...

  2. icape3 的使用

    在FPGA中,有时需要使用用户代码重配置FPGA,配置的内容可以是flash或者是其他的来源这样FPGA的启动模式有关,在本实验中配置文件是存放在flash中.实际的操作步骤如下: 1:生成一个工程, ...

  3. CCproxy 设置代理服务器。

    CCproxy 设置代理服务器. 通过代理服务器上网,出口IP就固定成代理服务器的IP 设置安装比较简单,直接去ccproxy官网下载就行 如果服务器是公网服务器,记得在设置,高级里面的网络中,把禁止 ...

  4. ModelAttribute注解使用与spring重定向传参

    @ModelAttribute可以用于修饰controller里的方法和参数,将被修饰的对象的值绑定到指定名称的属性里.当修饰方法时,方法返回的值会在该controller里每个访问处理前绑定一次.修 ...

  5. 史上最全的Maven Pom文件标签详解

    <span style="padding:0px; margin:0px"><project xmlns="http://maven.apache.or ...

  6. Oracle数据库备份与恢复命令

    备份:exp  用户名/密码@实例 file="e:\databack\XX_%date%.dmp" log = "e:\databack\XX8_%date%log.t ...

  7. iOS 通过URL网络获取XML数据的两种方式

    转载于:http://blog.csdn.net/crayondeng/article/details/8738768 下面简单介绍如何通过url获取xml的两种方式. 第一种方式相对简单,使用NSD ...

  8. MyBatis 注解式开发

  9. Linux下强大的查找命令find 用法和常见用例

    Linux系统下find是较为常用的指令,find命令在目录结构中搜索文件,并执行指定的操作,掌握它的形式与用法对我们很有用处. 因为Linux下面一切皆文件,经常需要搜索某些文件来编写,所以对于Li ...

  10. Motion Blur

    [Motion Blur] 此篇介绍最简单的全局Motion Blur.算法是将当前帧与前一帧按某一比例混合.这是一个过程,例如有10帧,在第1帧中,只有第1帧原图,第2帧中有1.2帧原图,第3帧中会 ...