Static scope and dynamic scope

Static scope refers to the section of code before it executes had defined its scope, in short, before you perform determines which local scope (variable) it can be applied.
Dynamic scope - scope of a function when the function is invoked before deciding

JavaScript uses lexical scoping that is static scope;

@ Static scoping:
var A = 10;

function fn() {
var b = 1;
console.log(a + b);
}

fn(); // 11

When you create fn function when it has been determined which variables can act, if the variable is a function fn which has a directly manipulated variable,
if not up to find one, which is static scope

@ Dynamic scope:
function foo () {
the console.log (A);
}

function bar() {
var a = 3;
foo();
}

There is 2;
bar(); // 2nd;

bar calls, bar which is called foo, foo function you need to find variables a, because the JavaScript is lexical scoping (ie static scope), foo. When parsed in the global scope
, we can only find a, output in the global scope 2 as a result, rather than a bar scope. If using js dynamic scope when you call foo bar, it will first check in a bar, the output is 3.

Guess you like

Origin www.cnblogs.com/xiaoniaohhl/p/11117715.html