Git Product home page Git Product logo

Comments (1)

SWbeginner avatar SWbeginner commented on June 12, 2024

谈到作用域链,先要理解执行环境。在JS中,执行环境定义了变量或函数有权访问的其他数据,决定了它们各自的行为。每个执行环境都有一个与之相关联的变量对象。环境中的所有变量和函数都保存在这个对象中。
全局执行环境就是最外围的一个执行环境(比如web浏览器中的window对象),当某个执行环境中的所有代码执行完毕后,该环境与保存在其中的所有变量和函数也随之销毁。
每个函数都有自己的执行环境,当执行流进入一个函数时,函数的环境就会被推入一个环境栈中。而函数执行之后,栈将其环境弹出,把控制权返还给之前的执行环境。
当代码在一个环境中执行时,会创建变量对象的一个作用域链(保证对执行环境有权访问的所有变量和函数的有序访问)。作用域链的最前端是当前执行代码所在环境的变量对象。如果这个环境是函数,则将其活动对象作为变量对象。活动对象最开始时只包含一个变量,即argumenets对象。作用域链中下一个对量对象来自包含环境,再下一个变量对象来自下一个包含环境。一直延续到全局执行环境。这就是作用域链。
标识符解析是沿着作用域链一级一级的搜索标识符的过程,搜索过程始终从作用域链的前端开始,然后逐级向后回溯,直至找到标识符为止。

from my-blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.