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 之执行上下文的生命周期 #12

Open
jejuin opened this issue Mar 28, 2020 · 0 comments
Open

JavaScript 之执行上下文的生命周期 #12

jejuin opened this issue Mar 28, 2020 · 0 comments
Labels
javascript Improvements or additions to documentation
Projects

Comments

@jejuin
Copy link
Owner

jejuin commented Mar 28, 2020

执行上下文的生命周期

Image

执行上下文的生命周期包括两个阶段:

  • 创建阶段(编译阶段):经过 JS 引擎编译后,会生成两部分内容:执行上下文(Execution context)和可执行代码。这个阶段是在代码执行前进行的,它的主要工作是:
    1. 创建变量对象
    2. 创建并初始化作用域链
    3. 确定 this 指向
  • 执行阶段:创建阶段之后,就会开始执行代码,这个时候会完成变量赋值,函数引用,以及执行其它可执行代码等。

我们以下面这段代码为例,分析执行上下文的整个生命周期。(示例来源于参考文章

var scope = "global scope";
function checkscope(){
    var scope = "local scope";
    function f(){
        return scope;
    }
    return f();
}
checkscope();
  1. 当开始执行上述代码时,会创建全局执行上下文;
ECStack.push(global_EC);

ECStrict = [
    global_EC
]
  1. 初始化全局执行上下文(创建阶段);
global_EC = {
    VO:window,
    //  current scope + scopes of all its parents
    // 这里将 scope 用 VO 表示,是因为在作用域中查找变量,其实就是从变量对象中查找,变量对象可以理解为作用域的实体
    scope: [VO],
    this: window
}

window = {
    // 内置全局属性和函数
    ... ,
    // 声明的全局变量和函数
    checkscope: reference to function checkscope(){},
    scope: undefined,
}
  1. 进入执行阶段,执行代码。当开始执行checkscope函数时,创建该函数的执行上下文。
ECStack.push(checkscope_EC);

ECStrict = [
    global_EC,
    checkscope_EC
]
  1. 初始化checkscope函数的执行上下文(创建阶段)。
checkscope_EC = {
    VO: {
        arguments: {
            length: 0
        },
        scope: undefined,
        f: reference to function f(){},
    }
    //  current scope + scopes of all its parents
    scope: [global_EC.VO, VO],
    // 非严格模式
    this: window
}
  1. 进入执行阶段,执行代码。当开始执行f函数时,执行过程同上,创建并初始化f函数的执行上下文;
ECStack.push(f_EC);

ECStrict = [
    global_EC,
    checkscope_EC,
    f_EC
]

f_EC = {
    VO: {
        arguments: {
            length: 0
        }
    },
    //  current scope + scopes of all its parents
    scope: [global_EC.VO, checkscope_EC.VO, VO],
    // 非严格模式
    this: window
}
  1. f函数执行完毕,其执行上下文出栈;
ECStack.pop();

ECStrict = [
    global_EC,
    checkscope_EC
]
  1. checkscope函数执行完毕,其执行上下文出栈;
ECStack.pop();

ECStrict = [
    global_EC
]
  1. 当关闭浏览器或者当前浏览器窗口,全局执行上下文才出栈。

参考:

JavaScript深入之执行上下文

@jejuin jejuin added the javascript Improvements or additions to documentation label Mar 30, 2020
@jejuin jejuin added this to 执行机制 in JavaScript Mar 30, 2020
@jejuin jejuin closed this as completed Nov 19, 2020
@jejuin jejuin reopened this Nov 20, 2020
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
No open projects
JavaScript
执行机制
Development

No branches or pull requests

1 participant