scope in js

    1. Global scope: 1. JS code directly written in the script tag,  
                      2. Code life cycle directly written in the external js file
                      : created when the page is opened, and destroyed when the page is closed
                      . Global variables: in any function The global variables that are valid in the domain
                      are properties under the window object. All properties and methods under the window object can be omitted window
        2. Local scope: only functions can generate local scope
                      Declaration cycle: start to create when calling, and end the life cycle after the function call is completed
                      Local variables: only in the current Valid in scope

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>作用域</title>
    <script src="./b.js"></script>
</head>

<body>

</body>
<script>
    /* 
       如何判断何时局部作用域:只需判断变量在全局中获取不到
    */
    var a = 100;
    console.log(window.a);
    console.log(str);
    console.log(a);
    // 1、
    while (true) {
        var num1 = 1;
        break;
    }
    console.log(num1);
    console.log(window.num1);

    // 2、
    if (1) {
        var num2 = 10;
    }
    console.log(num2);
    // 3、
    for (var i = 0; i < 1; i++) {
        var num3 = 100;
    }
    console.log(num3);
    // 4、
    var key = 1;
    switch (key) {
        case 1:
            var num4 = 1000;
            break;

        default:
            break;
    }
    console.log(num4);
    // 5、
    do {
        var num5 = 10000;
        key++;
    }
    while (key <= 2);
    console.log(num5);
    // 6、
    function fn1() {
        var num6 = 0.1;
        console.log(num6);
    }
    fn1();
    // console.log(num6);
    var x1 = 2.36;
    console.log(window);
    console.log(window.x1);

    function fn2() {
        console.log(x1);
        // console.log(num6);
    }
    fn2()
    /*
      三、隐式的全局变量(重点,考试要考!)
        1、函数中没有用var声明的变量
        2、函数中没有用var声明的多个变量,用分号隔开的
        3、函数中没有用var声明的多个变量,用赋值号链接的
    */

    function func1() {
        var x = 1,
            y = 2,
            z = 3; //x,y,z都是局部变量
        var m = 1;
        n = 2;
        q = 3; //n,q是隐式全局变量
        var e = f = g = 1; //f,g是隐式全局变量
    }
    func1();
    console.log(x);
    console.log(count); //-3

    str1 = "嘿嘿";
    console.log(str1);
    // console.log(x);
    // console.log(y);
    // console.log(z);
    // console.log(m);
    console.log(n);
    console.log(q);
    // console.log(e);
    console.log(f);
    console.log(g);

    /*
      四、作用域链的查找规则:
          先从当前的作用域内查找,如果有就返回
          如果没有,再上一级作用域内查找,依次向上查找,
          如果还没有,从全局作用域内查找。
          全局中还没有就报错
    */
    // var number1 = 0.111;
    function f1() {
        // var number1 = 0.222;
        function fn2() {
            // var number1 = 0.333;
            console.log(number1);
        }
        fn2()
    }
    f1();
</script>

</html>

Guess you like

Origin blog.csdn.net/weixin_47619284/article/details/126787679