1 for (var i = 0; i < i++) {2 setTimeout (function() {3 Console.log (i); // output 10 10, because the SetTimeout method is asynchronous, when executed to settimeout, the For loop has been executed, the I variable value is ten 4 }, (+); 5 }
1 for(vari = 0; I < 10; i++) {2(function(a) {3 //the value of the variable i is copied to a when passed to this scope,4 //so this value doesn't change with external variables.5SetTimeout (function() {6Console.log (a);//Output 0 1 2 3 4 5 6 7 8 97}, 1000);8}) (i);//here we pass in parameters to "closure" variables9}
1 //a declarative function definition statement is a top-level scope, and a function call statement can be written before a function declaration statement:2Add (1, 2);//33 functionAdd (m, n) {4Alert (m +n);5 }6 7 8 //functions defined by function expressions cannot be called until they are defined:9 //Typeerror:add is not a functionTenAdd (1, 2); one varAdd =function(m, N) { aAlert (m +n); -};
JavaScript Knowledge Point Notes