JS function scope and the scope chain

Variable Scope

In JavaScript the scope of global variables is relatively simple, its scope is global, anywhere in the code are all defined. However, the function parameters and local variables defined only in the body of the function. In addition to the priority of local variables than global variables of the same name, the same name that is when local variables and global variables, local variables will overwrite global variables.

 var num = 1;            //声明一个全局变量
    function func() {
        var num = 2;        //声明一个局部变量
        return num;
    }
    console.log(func());    //输出:2 
    console.log(num);      //输出:1

 Note: Be sure to use var when declaring a local variable, otherwise, the interpreter as the variable properties of the global window object.

Function scope

In the scope of variables in JavaScript, and not C, Java and other programming languages ​​like too, in addition to the variable declaration section of the code is not visible, we usually called block-level scope, however, for use in JavaScript is a function of the role domain (variables declared their functions thereof as well as any function of the body is the body of the function definition is nested).

function func() {
        console.log(num);           //输出:undefined,而非报错,因为变量num在整个函数体内都是有定义的
        var num = 1;                //声明num 在整个函数体func内都有定义
        console.log(num);           //输出:1
    }
    func();

Note: The function scope in JavaScript refers to all variables declared within a function in the body of the function is always visible, that is to say before the function body of variable declarations already available.

As a variable attribute

When declare a global variable, in fact, it is the definition of a property of the global object window.

    var num = 1;            //声明全变量num
    alert(window.num)       //输出:1 声明的全局变量实际上就是声明了一个window对象的属性

javascript two scopes: global scope; local scope / function scope;
the name implies, a global variable in the global scope, the local variable in local scope.
All global variables web page scripts and functions can be used. If the variable is not within the function with the var statement it is also a global variable, but not recommended.

The scope chain

Scope chain is a variable to find the time to find the track formed upward layer by layer.

Each function has its own execution environment, internal environment stores all variables defined scope. When a function is executed, the environment is pushed into the stack environment, after the implementation of pop-up environment, to return before the execution environment.

The role of the scope chain is to ensure the right to access to the execution environment of orderly access to all variables and functions, front-end scope, is always variable object code where the current execution environment (that is, code that recently)

function foo(a) {
    var b = a * 2;
    function bar(c) {
        console.log( a, b, c );
    }
    bar(b * 3);
}
foo( 2 ); // 2 4 12

Parsing the identifier to identifier search process along scope chain stage by stage. Search process always starts from the scope chain to the front, back then progressively backwards until you find the identifier (if identifier is not found, often leads to errors) - "JavaScript Advanced Programming"

Reference: https://blog.csdn.net/qq_33939113/article/details/82717240
https://www.cnblogs.com/mrzl/p/4415149.html
https://cloud.tencent.com/developer/article/ 1339323

Guess you like

Origin www.cnblogs.com/jessie-xian/p/11596108.html