For loops are available in most languages. In javascript, The for loop has several different usage situations. The following describes the loop statements that are frequently used in program implementation. The for loop is available in most languages. In javascript, The for loop has several different usage situations. The following describes my understanding respectively.
First: (normally, perform related operations cyclically)
The Code is as follows:
Var objA = document. getElementsByTagName ("");
Var I, max;
For (I = 0, max = objA. length; I ObjA [I]. onclick = function (){
Alert (this. innerHTML );
}
}
Loop, register the click operation of the hyperlink tag in sequence
Type 2: (Object-specific and Object-specific operations)
The Code is as follows:
Var person = {name: 'wmhel', age: '28 '};
Var tips = ''; for (var obj in person ){
Tips + = obj + '-->' + person [obj] + '\ N'
}
Alert (tips)
Third: (used in arrays to perform specific operations on arrays)
The Code is as follows:
Var num = [1, 3, 5];
Var total = 0;
Num. forEach (function (e ){
Total + = e;
});
Alert (total );
This forEach loop is valid in firefox and chrome.