markdown JS执行上下文

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown JS执行上下文相关的知识,希望对你有一定的参考价值。

## Gobal Execution Context

Execution Contexts allow the JavaScript engine to manage the complexity of interpreting and running your code.

The only time an Execution Context is created is when the JavaScript engine first starts interpreting your code (Global Execution Context) and whenever a function is invoked.

1. How do they get created?

    1. The first Execution Context that gets created when the JavaScript engine runs your code is called the “Global Execution Context”.
    2. Consists of two things:
        1. A global object
        2. variable called "this"
            1. this will reference the global object which will be window if you’re running JavaScript in the browser or global if you’re running it in a Node environment.

1. What do they consist of?

    1. Creation Phase (Global Creation Phase)
        1. Create a global object
        2. Create an object called "this"
        3. Set up memory space for variables and functions
        4. Assign variable declarations a default value of "undefined" while placing any function declations in memory
            1. Hoisting
                1. This process of assigning variable declarations a default value of undefined during the creation phase
                2. Nothing is actually hoisted or moved around. 
        
    2. Execution Phase
        1. It’s not until the Execution phase where the JavaScript engine starts running your code line by line and executing it and assigns the real values to the variables already living in memory.
        
## Function Execution Context
  
Created whenever a function is invoked

1. What does it consist of?

    1. Creation Phase
        1. Creates an arguments object
        2. Creates an object called "this"
        3. Set up memory space for variables and functions
        4. Assign variable declarations a default value of "undefined" while placing any function declarations in memory.
        
    2. Execution phase
        1. Anytime a function is invoked, a new Execution Context is created and added to the Execution Stack.
        2. Whenever a function is finished running through both the Creation and Execution phase, it gets popped off the Execution Stack.
        
## Scope

The current context of execution. Where variables are accessible.

1. Scope Chain
    1. If the JavaScript engine can’t find a variable local to the function’s Execution Context, it’ll look to to nearest parent Execution Context for that variable.
    
2. Closure Scope
    1. the inner function is nested inside of the outer function, so inner creates a Closure over the outer variable environment.

以上是关于markdown JS执行上下文的主要内容,如果未能解决你的问题,请参考以下文章

markdown JavaScript中的执行上下文,提升,范围和闭包

markdown 瓶确保进口的JS在jQuery的之后执行

JS中的执行上下文

js-执行上下文

JS执行上下文

关于JS闭包