In ECMAScript, all declared variables seen outside the function at the function declaration can be accessed inside the function.Final value!
Closure functions can only access the final value of a variable !!!
Eg:
Function fnTest (arr ){
For (var I = 0; I <arr. length; I ++ ){
Arr [I] = function () {alert (I + "|" + arr [I]);};
}
}
Var arr = [0, 1, 2, 3];
FnTest (arr );
For (var I = 0; I <arr. length; I ++ ){
Arr [I] (); // always output 4 and an undefinedBecause the I value is 4 after the function exits, the accessed value is only 4
// The result is continuous.Four "4 | undefined" are displayed"
}
Not only can the variable values outside the closure be accessed in the closure, but also its values can be set.
Eg:
Function fnTest (){
Var a = "June ";
Return {
Set: function (param ){A = param},
Get: function () {return}
};
}
Var obj = fnTest ();
Alert (obj. get ());//June is displayed.
Obj. set (586 );
Alert (obj. get ());//586