Deep understanding of the for statements in javascript _ javascript skills

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.