I understand the scope

JavaScript uses static scope: Also called lexical scope, it is time to define the scope of a function on the determined; (because JavaScript uses lexical scoping, the scope of functions of location-based function to create.)

was value = 1;

function foo() {
    console.log(value);
}

function bar() {
    was value = 2;
    foo();
}

bar();

// result 1
Analysis, from top to bottom is not performed when the value foo in global conditions, in this case the value is equal to 1, with the foo performed without the bar, the value becomes 2;
So is static scope, function declarations when he determined the scope
Such analysis This article can also https://github.com/mqyqingfeng/Blog/issues/3
perform the function foo, foo start internal function to find whether there is a local variable value, if not, depending on the position of writing, a look above the code layer, i.e. equal to value 1, so that a printed result.

  

Guess you like

Origin www.cnblogs.com/qqfontofweb/p/11932792.html