About the execution environment and scope of the function

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>


</body>
</html>
<script >
    //Code execution: generate an execution environment first, then execute
    //Global scope (global variables):
    //1, the outermost function and the variables defined outside the outermost function have global scope, the following example :sum=10, test, a=1
    //The function itself is also a special variable, its name is the function name
    //2, variables that are not directly assigned are automatically declared to have global scope -- such as all1 all2
    //3 , all the properties of the window object have a global scope, for example: the built-in properties of the window object have a global scope, such as window.name, window.location, window.top, etc.
    //We can also define a global variable as follows: window. name=11
    //The execution environment is similar to a table: global variables are placed on the first line, and local variables are placed on the second line.
    //Variable search mechanism: search for local variables first, if not found, search for all variables
// first line: sum, test, a
// Second line: a, foo
    /*Look at the execution of the function*/
    /*a: Due to variable declaration hoisting, the a variable exists here, but it is undefined*/
    var sum = 0;
    var a = 10
    all1 = 1000;
    function test() {
        // At this time, due to the promotion of the variable declaration, the a variable has been declared, and the value is undefined
        console.log(a);
        all2 = 200;
        //foo is a function, which will be raised during parsing, so it can access
        console.log(foo( ));
        /*What is the output if the following sentence is omitted*/
        //var a =1;
        function foo() {
            return 2;
        }
    }
    test();
</script>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324693116&siteId=291194637