Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scope chain #6

Open
DeanTG opened this issue Feb 1, 2023 · 9 comments
Open

scope chain #6

DeanTG opened this issue Feb 1, 2023 · 9 comments

Comments

@DeanTG
Copy link
Owner

DeanTG commented Feb 1, 2023

作用域最大的用处就是隔离变量

@DeanTG
Copy link
Owner Author

DeanTG commented Feb 1, 2023

作用域在函数定义时就已经确定了。而不是在函数调用时确定

@DeanTG
Copy link
Owner Author

DeanTG commented Feb 1, 2023

作用域中变量的值是在执行过程中产生的确定的,而作用域却是在函数创建时就确定了。

@DeanTG
Copy link
Owner Author

DeanTG commented Feb 1, 2023

如果要查找一个作用域下某个变量的值,就需要找到这个作用域对应的执行上下文环境,再在其中寻找变量的值。

@DeanTG
Copy link
Owner Author

DeanTG commented Feb 1, 2023

变量取值,要到创建这个函数的那个作用域中取值——是“创建”,而不是“调用”,这就是所谓的“静态作用域”(词法作用域)。

@DeanTG DeanTG changed the title 作用域 scope chain Feb 1, 2023
@DeanTG
Copy link
Owner Author

DeanTG commented Feb 1, 2023

作用域类型:

  • 全局作用域
  • 函数作用域
  • 块级作用域

@DeanTG DeanTG changed the title scope chain scop Feb 2, 2023
@DeanTG DeanTG changed the title scop scope Feb 2, 2023
@DeanTG DeanTG changed the title scope scope chain Feb 2, 2023
@DeanTG
Copy link
Owner Author

DeanTG commented Feb 2, 2023

作用域链由当前执行环境的变量对象(未进入执行阶段前)与上层环境的一系列活动对象组成,它保证了当前执行环境对符合访问权限的变量和函数的有序访问。

@DeanTG
Copy link
Owner Author

DeanTG commented Feb 2, 2023

作用域链本质上是一个指向变量对象的指针列表,它只引用不包含实际变量对象,是作用域概念的延申。作用域链定义了变量在当前上下文访问不到的时候如何沿作用域链继续查询的一套规则。

@DeanTG
Copy link
Owner Author

DeanTG commented Feb 2, 2023

对于外部环境的引用意味着在当前执行上下文中可以访问外部词法环境。也就是说,如果在当前的词法环境中找不到某个变量,那么Javascript引擎会试图在上层的词法环境中寻找。(Javascript引擎会根据这个属性来构成我们常说的作用域链)

@DeanTG
Copy link
Owner Author

DeanTG commented Feb 2, 2023

Javascript中的作用域说的是变量的可访问性和可见性

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant