当前位置:网站首页>JS pre parsing

JS pre parsing

2022-06-24 18:36:00 Brother Mengfan

Catalog

One 、 What is pre parsing

Two 、 Pre parse

2.1、 Variable pre parsing

2.2、 Function pre parsing

3、 ... and 、 Statement notes

Four 、 Variable promotion reason


One 、 What is pre parsing

Pre resolution refers to that under the current scope ,JavaScript Before code execution , The browser will default to include var and function The sound
Explicit variables are declared or defined in memory in advance .
Example :
    <script>
        a = 2;
        var a;
        console.log(a);//2
    </script>

Two 、 Pre parse

2.1、 Variable pre parsing

Variable pre parsing is also called variable promotion , It means that the declaration of variables will be promoted to the top of the current scope , But the assignment of variables will not improve .
Example 1:
    <script>
        console.log(age); // undefined 
        var age = 18;
    </script>
The above code will output undefined Instead of reporting a mistake , Why? ?
This is caused by variable pre parsing .
After pre analysis :

  Example 2:

  After pre analysis :

2.2、 Function pre parsing

Function pre parsing is also called function promotion , It means that the function declaration will be promoted to the top of the current scope , But it doesn't call the function .
Example 1:

  Example 1 Function pre parsing process :

  Example 2:

  Example 2 Function pre parsing process :

  Be careful : If you use a function expression to declare a function , Will not be promoted .

3、 ... and 、 Statement notes

(1) Variable declaration and function declaration , Variable declaration promotion takes precedence over function declaration promotion . however , A function declaration overrides an undefined variable with the same name .

Example :

Pre parsing process :

 

 (2) Variable declaration and function declaration , Variable declaration promotion takes precedence over function declaration promotion . If the variable has a value , Will not be covered .

Example :

  Pre parsing process :

 (3) The following function declaration will override the previous one

    <script>
        function fn() {
            console.log('hello');
        }

        function fn() {
            console.log('world');
        }
        fn(); //world
    </script>

summary : On declaration ( Whether it's a variable or a function ) Try not to take the same identifier ( name ).

Four 、 Variable promotion reason

In the compilation phase , The engine declares variables and functions , But the variable will not be assigned , This is mainly due to performance considerations . Variable is declared , But it may not be used later , If it is not used but assigned a value , It's just a waste of memory . In short ,var a This code takes place during the compilation phase , and =1 This code will be based on the actual situation , Occurs during the execution phase , This is the same. " Variable Promotion " Why . Another thing to note , The function declares the whole function body ( Because the function declares that there is no assignment operation ), And the priority is higher than the variable with the same name .
Example :
    <script>
        console.log(fn()); // Output :1 \
        console.log(n); // Output :undefined 
        function fn() {
            return 1;
        }
        var n = 2;
    </script>
In browsing, it is pre resolved as follows :
    <script>
        var a;

        function fn() {
            return 1;
        }
        console.log(fn());
        console.log(n);
        n = 2;
    </script>

原网站

版权声明
本文为[Brother Mengfan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211333166478.html