JavaScript few topics to understand the scope, the scope chain, pre-parsing rules, the expression

Look at the title

1, the result is undefined

    console.log(a)
    var a = 1
复制代码

2、报错 Uncaught ReferenceError: Cannot access 'a' before initialization

    console.log(a)
    let a = 1
复制代码

3、报错 Uncaught ReferenceError: a is not defined

    console.log(a)
    a = 1
复制代码

4, the result ƒ a () {a = 4}, 1, 1, 3, 3

    console.log(a)
    var a = 1
    console.log(a)
    function a() {a=2}
    console.log(a)
    var a = 3
    console.log(a)
    function a() {a=4}
    console.log(a)
复制代码

5、结果 ƒ a() {console.log(a=4)} 、 1 、1 、3 、3、 Uncaught TypeError: a is not a function

    console.log(a)
    var a = 1
    console.log(a)
    function a() {a=2}
    console.log(a)
    var a = 3
    console.log(a)
    function a() {console.log(a=4)}
    console.log(a)
    a()
复制代码

6、1

<script>
    var a = 1
</script>
<script>
    console.log(a)
</script>
复制代码

7、Uncaught ReferenceError: a is not defined

   <script>
        console.log(a)
    </script>
    <script>
        var a = 1
    </script>
复制代码

8、undefined、 1

   var a = 1
    function f() {
        console.log(a)
        var a = 2
    }
    f()
    console.log(a)
复制代码

9、1、 2

    var a = 1
    function f() {
        console.log(a)
        a = 2
    }
    f()
    console.log(a)
复制代码

10、undefined、1

    var a = 1
    function f(a) {
        console.log(a)
        a = 2
    }
    f()
    console.log(a)
复制代码

11、1、1

    var a = 1
    function f(a) {
        console.log(a)
        a = 2
    }
    f(a)
    console.log(a)
复制代码

Browser resolution process

1, pre-parsed

Get var function parameters, variables given undefined, function block function for the entire script function from inside to outside the top-down

2, the progressive code that interprets

Expressions (+ - * /% + -! Parameters) can modify the value of pre-parsed out, the function declaration is not an expression, the function call is the local domain

Reproduced in: https: //juejin.im/post/5ce4e808518825333d1b9248

Guess you like

Origin blog.csdn.net/weixin_33860528/article/details/91430771