scope
JS uses static scope (lexical scope) to find definitions rather than find implementations. The scope of a function is determined at the time it is defined
var a = 1;
function check(){
var a = 2;
function f(){
return a;
}
return f();
}
checkscope();/ / 2
Copy the code
perform
There are three types of code in JS: global code, function code, and eval code
When a piece of code is encountered, the operation creates the corresponding execution context stack, including: variable object (VO), this, scope chain
When a function is executed, an execution context is created and pushed onto the execution context stack. When the function is finished, the execution context of the function is ejected from the stack
function fun3() {
console.log('fun3')}function fun2() {
fun3();
}
function fun1() {
fun2();
}
fun1();
Copy the code
/ / pseudo code
// fun1()
ECStack.push(<fun1> functionContext);
// call fun2 in fun1 to create the execution context of fun2
ECStack.push(<fun2> functionContext);
// fun2 also calls fun3!
ECStack.push(<fun3> functionContext);
// fun3 is executed
ECStack.pop();
// fun2 completes execution
ECStack.pop();
// fun1 completes execution
ECStack.pop();
// Javascript then executes the following code, but there is always a globalContext at the bottom of the ECStack
Copy the code