recursive
- The function calls itself
- The function goes to the exit, gets the value, and then goes up one step at a time to get the final result
- Performance is poor because you are waiting for a value
- N factorial
function fn1(a) {
if (a === 1) return 1;
return a * fn1(a-1);
}
console.log(fn1(5));
Copy the code
- Implement the Fibonacci sequence
// 1 1 2 3 5 8 13
function fn2(n){
if(n === 1 || n === 2) return 1;
return fn2(n - 2) + fn2(n - 1);
}
console.log(fn2(5));
Copy the code
precompiled
- Js execution process
- Check for syntax errors
- precompiled
- Explain a line, execute a line
- Imply Global variables
- A global variable that is assigned a value without being declared
b = 2; // window.b === 2;
Copy the code
- Precompile time functions declare overall promotion, variables only declare promotion, not assignment.
var a = 2; // 1. Declare a variable 2.2 to a.
Copy the code
- Precompile the solution
- Code, whether executed or not, generates a variable boost during precompilation. For example:
if(){}
The statement in, even if the condition is not met, produces a variable promotion - Precompile in function
- AO (Activation Object active object function context)
- Look for parameters and variable declarations in functions
- The argument value is assigned to the parameter
- Find the declaration of the function body, and assign the function body
- Executive function
- Problem sets
- AO (Activation Object active object function context)
- Code, whether executed or not, generates a variable boost during precompilation. For example:
function test(a){
console.log(a);
var a = 1;
console.log(a);
function a(){};
console.log(a);
var b =function (){};
console.log(b);
function d(){};
}
test(2);
// AO = {
// a : undefined -> 2 -> function(){... } - > 1
// b : undefined -> function(){... }
// d : function(){... }
/ /}
// function a(){... }, 1, 1, function b(){... }
Copy the code
function test(a, b){
console.log(a);
c = 0;
var c;
a = 5;
b = 6;
console.log(b);
function b(){};
function d(){};
console.log(b);
}
test(1);
// AO = {
// a : undefined -> 1 -> 5;
// b : undefined -> function b(){... } - > 6;
// c : undefined -> 0;
// d : function d(){... }
// }
// 1,6,6
Copy the code
- GO Global Context (Window)
- Looking for a variable
- Find a function declaration
- perform
- Problem sets
console.log(a, b);
function a(){}
var b = function (){}
// GO = {
// b: undefined -> function (){}
// a: function a(){... }
// }
// function a(){... } undefined
Copy the code
var b = 3;
console.log(a);
function a (a){
console.log(a);
var a = 2;
console.log(a);
function a (){}
var b = 5;
console.log(b);
}
a(1);
// GO = {
// b: 3
// a: function a(){....}
// }
// AO ={
// a: undefined -> function a(){... } - > 2,
// b: 5
// }
// function a(){.... }, function a(){... }, 2, 5
Copy the code
a = 1;
function test(){
console.log(a);
a = 2;
console.log(a);
var a = 3;
console.log(a);
}
test();
var a;
// GO = {
// a : undefined ,
// test : function test(){... }
// }
// AO = {
// a : undefined, 2, 3
// }
// undefined, 2, 3
Copy the code
function test(){
console.log(b);
if(a){
var b = 2;
}
c = 3;
console.log(c);
}
var a;
test();
a = 1;
console.log(a);
// GO = {
// a : undefined
// test: function(){... },
// c : 3
// }
// AO = {
// b : undefined
// }
// undefined, 3 ,1
Copy the code
function test(){
return a;
a = 1;
function a(){}
var a = 2;
}
console.log(test());
// AO = {
// a: function a(){...}
// }
// function a(){... }
Copy the code
function test(){
a = 1;
function a (){}
var a = 2;
return a;
}
console.log(test());
// AO = {
// a: function a(){...}, 1, 2
// }
/ / 2
Copy the code
a = 1; function test(e){ function e(){} arguments[0] = 2; console.log(e); if(a){ var b = 3; } var c; a = 4; var a; console.log(b); f = 5; console.log(c); console.log(a); } var a; test(1); // G0 = { // a: 1, // test: function test(e){... } // f: 5 // } // AO = { // e: undefined -> 1 -> function e(){... } -> 2, // b: undefined, // c: undefined, // a: undefined -> 4, // // } // 2, undefined, undefined, 4Copy the code