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

JavaScript深入之作用域链 #5

Open
huiyiwanan opened this issue Jan 16, 2023 · 0 comments
Open

JavaScript深入之作用域链 #5

huiyiwanan opened this issue Jan 16, 2023 · 0 comments
Labels
Javascript 深入系列 Improvements or additions to documentation

Comments

@huiyiwanan
Copy link
Owner

huiyiwanan commented Jan 16, 2023

前言

当JavaScript代码执行一段可执行代码(executable code)时,会创建对应的执行上下文(execution context)。

对于每个执行上下文,都有三个重要属性:

  • 变量对象(Variable object,VO)
    -作用域链(Scope chain)
  • this
    今天重点讲讲作用域链。

作用域链

函数创建-[[scope]],函数创建时,就会保存所有父变量对象到其中。

function foo() {
    function bar() {
        ...
    }
}

函数创建时,各自的[[scope]]为:

foo.[[scope]] = [
  globalContext.VO
];

bar.[[scope]] = [
    fooContext.AO,
    globalContext.VO
];

函数激活

Scope = [AO].concat([[Scope]]);

下一篇文章

JavaScript深入之从ECMAScript规范解读this

@huiyiwanan huiyiwanan added the Javascript 深入系列 Improvements or additions to documentation label Jan 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Javascript 深入系列 Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant