以下Javascript执行顺序是否正确?

时间:2016-03-09 12:38:52

标签: javascript

编码noob试图理解JS。我有以下简单的代码,并试图详细分解正确的执行顺序。

function b() {
    console.log(myVar)
}

function a() {
    var myVar = 2;
    b();
}

var myVar = 1;
a();

我试图将其分解为

1.      Global Creation Phase started  where JS engine searches for all new variables and functions to be created..
        First search for all new variables sitting on the Global stage

2.      finds one variable called myVar
        creates window.myVar memory space and assigns "undefined" as its value
        window.myVar is NOT assigned 1 as its value at this stage!!!
        finds no more new variables to be created

3.      now searches for all new functions sitting on the Global stage to be allocated a memory space
        finds two functions b( ) and a( ) to be created

4.      it creates window.b( ) memory first since it lexically sits before a( )
        4.1     new local creation phase started
                searches for new variables
                it finds no new variables to be created
        4.2     it searches for new functions
                it finds no new functions to be created
        4.3     local execution phase started
                creates memory space for console.log using window.myVar value that it found from the Global stage which is at this moment is "undefined"
                it does not log the return value to the console yet....
        finish window.b( ) memory creation phase

5.          Start window.a( ) memory creation phase
        5.1     all new creation phase started
                searches for all new variables
                finds var myVar command and creates a.myVar memory space and assigns "undefined" as its value
                a.myVar is  NOT assigned the value 2 at this stage !!!
        5.2     searches for all new functions
                finds no new function
        5.3     local execution phase started
                runs window.b( ) and stores the whatever the return value of window.b( ) into the return value of window.a( ) memory...  which is to log "undefined" into the console
                it still does not log its return value to console 
        finish window.a() memory creation phase

6.      all local creation phase  local execution phase and Global creation phase finished

7.      Starts Global execution phase

8.      Finds myVar = 1 command and assigns value 1 to window.myVar 

9.      finds a( ) command and execute function window.a( )

10.     goes into window.a( ) function and assigns value 2 to a.myVar
        finds b( ) command and executes window.b( ) passing window.myVar's value of 1 since window.myVar is what the window.b() return value is waiting for...  not a.myVar value
        receives the command to log 1 into console from function window.b( ) and sends it back to window.a( ) return value 

11.     back to global stage: window.a( ) function receives the return value which is:  log 1 to the console 

12.     logs 1 to the console and finished....  phew!!!!  

任何人都可以评论此执行顺序是否正确...尤其是何时分配了变量值...谢谢......

1 个答案:

答案 0 :(得分:3)

我不同意的两件事

  • 2。之前,还有两个变量在全局级别定义了 a b 的函数类型。

  • 此外,在 4。 5。中,相应的本地创建阶段不会启动,直到它们被调用。< / p>