Do you remember the original native JS from vue and React? Ashamed of my today began to review the original knowledge, but also incidentally some notes, for later often recall, notes are watching the teaching video copy, if there is a mistake, welcome to correct
O factorial
function nums(a){
if(a === 1 || a === 0) {return 1;
}
return a*nums(a- 1)}console.log(nums(5)) / / 120
Copy the code
Fibonacci numbers
function nums(a){
if(a === 1) {return ;
}
return nums(a- 1)+nums(a2 -)}console.log(nums(5))
Copy the code
precompiled
JS execution process
1. Grammatical analysis
2. The precompiled
3. Explain execution
Ii. The precompilation process
1. Simple rules:
Function declaration, overall promotion
Variable declaration, declaration of promotion
2. Compilation process
1. Create an AO object
2. Find the parameter and variable declarations and replace the parameter and parameter names with the AO attribute name value bit undefined
3. Unify the parameter values and parameters
4. Find the function declaration inside the function body and assign the value to the function body
Example 3.
function a(a){
console.log(a) // function
var a = 123
console.log(a) / / 123
function a(){}
console.log(a) / / 123
var b = function (){}
console.log(b) //function
}
a(1)
// Perform step analysis
//1. Create AO objects
AO {
}
//2. Find the parameter and variable declarations and replace the variable and parameter names with the AO attribute name value bit undefined
Ao {
a: undefined.b: undefined
}
//3. Unify the argument values and parameters
Ao {
a: 1 , // a is first assigned to the value 1 of the calling parameter
b: undefined // Function B is not promoted because function b is a function expression and only promotes variables
}
// 4. Find the function declaration in the function body, the value assigned to the function body
Ao {
a: 1 -> function a(){}, // function a(){
b: undefined // Function B is not promoted because function b is a function expression and only promotes variables
}
Copy the code
Example 4.
function test (a, b){
console.log(a) // The first 1
c = 0
var c;
a = 3;
b = 2;
console.log(b) // The second 2
function b(){}
function d(){}
console.log(b) // The third 2
}
test(1)
Copy the code
Example 5.
function test (a, b){
console.log(a) // function a(){}
console.log(b) // undefined
var b = 234
console.log(b) / / 234
a = 123
console.log(a) / / 123
function a(){}
var a;
b = 234;
var b = function(){}
console.log(a) / / 123
console.log(b) // function(){}
}
test(1)
Copy the code
6. Sample
global = 100;
function fn(){
console.log(global) // undefined
global = 200
console.log(global) / / 200
var global = 300
}
fn()
Copy the code
Example 7.
function test(){
console.log(b) //undefined
if(a){
var b = 100
}
c = 123
console.log(b) //undefined
}
var a;
test();
a = 10;
console.log(c) / / 123
Copy the code
8. Implicit conversion
console.log(typeof(a)) // "undefined"
console.log(typeof(null)) //object
/ /!!!!!" " => true
/ /!!!!!" " => false
if(typeof(a)&& -true + (+undefined) +"") {/** * typeof(a) 'undefined' * -true -1 * +undefined NAN * -true + (+undefined)+"" "NAN" **/
// So the entire IF statement is true
console.log(1)}Copy the code
closure
But any function inside is saved outside, forming a closure
function a(){
var aa = 0
function b(){
aa++
console.log(aa)
}
return b;
}
// a()() 1
// a()() 1
let demo = a()
demo() / / 1
demo() / / 2
Copy the code
function test(){
var num = 100
function a(){
num++
console.log(num) / / 101
}
function b(){
num--
console.log(num) / / 100
}
return [a,b]
}
var mtArr = test()
mtArr[0]()
mtArr[1] ()Copy the code
Execute function immediately
(function(){
var a= 123
var b= 456
console.log(a+b)
}())
+function(){} ()var test = function(){} ()// This cannot be executed and an error is reported
function(){
}()
// This cannot be executed, but no error is reported
function(a,b,c){},1.2.3)
Copy the code
function test (){
var arr = []
for(var i=0; i<10; i++){ arr[i] =function(){
console.log(i)
}
}
return arr;
}
var myArr = test()
myArr[0] ()/ / 10
myArr[1] ()/ / 10
/ / rewrite
function test (){
var arr = []
for(var i=0; i<10; i++){ (function(i){
arr[i] = function(){
console.log(i)
}
}(i))
}
return arr;
}
var myArr = test()
myArr[0] ()/ / 0
myArr[1] ()/ / 1
Copy the code