JS variable lift mechanism function

Read the blog Gangster operating principle JavaScript parsing engine , the code segment where curiosity.

        <script>
            console.log(a); // 打印[Function: a]
            var a = 1;
            function a(){console.log(2);}
            console.log(a);
            var a = 3;
            console.log(a);
            function a(){console.log(4);}
            console.log(a)
        </script>
————————————————
版权声明:本文为CSDN博主「开心大表哥」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/a419419/article/details/82906049

JS engine mechanism complement the results from the brain. . .

  1. JS function declaration should be a function declaration (C function declaration inside the like: [return type] [function name] (parameter list);) + functions thereof. Why does this function unlike variables declared and assigned a two-step, I think is one of the JS function, did not like to declare a function name and then define the function code, and if so, it is not called the function declaration direct expression is a function of bad mo. Why should it, because I do not know where official documents. . .

  2. Order after the function declarations and variable lift: guess after 1-- function before variables; variables before and after the function 2-- guess; guess 3-- will not be in two places ah respectively. . . I have not found an official document, the JS engine with the realization of the bar. . . No matter, it does not affect my next guess. . .

  3. When the functions and variables of the same name (called Xiao Ming, right), as long as unassigned variables, Xiao Ming (type) will still function, once the variable assignment, (type of) Xiaoming according to changes in the value being assigned. That variable assignment overrides Xiao Ming inside.

Guessing at the principle might be as follows Jiang Zi. . .

harm. . . I first recorded it, help them understand. . . I guess it should be no one sees. . . If you do not care to see, please do not take it too seriously it rather

Guess you like

Origin www.cnblogs.com/xw172854/p/12424348.html