The internal function of a function in js can still access the variables defined in this function after the function is executed. This is called Closure)
The Code is as follows:
Function outside ()
{
Var myVar = 1;
Return function (){
Alert (myVar );
}
}
Var t = outside ();
T ();
The internal function of a function in js can still access the variables defined in this function after the function is executed. This is called Closure)
The following example adds an onclick event to three anchor1, anchor2, and anchor3. The expected effect is that the corresponding anchor4 is displayed when you click the anchor3 ", this is the logic bug caused by the closure. After the function of the add event is executed, the I value is 4,
The Code is as follows:
ADS. addEvent (window, 'load', function (W3CEvent) {m
For (var I = 1; I <= 3; I ++ ){
Var anchor = document. getElementById ('anchor' + I );
ADS. addEvent (anchor, 'click', function (){
Alert ('My id is anchor '+ I );
});
}
});