Javascript foreach generic looping Method

Source: Internet
Author: User
Tags exit in

CopyCode The Code is as follows: var foreach = (function (){
// Array and pseudo array Traversal
VaR _ array_foreach = function (array, block, context ){
If (array = NULL) return;
// Special processing of string
If (typeof array = 'string '){
Array = array. Split ('');
}
VaR I = 0, length = array. length;
For (; I <length & block. Call (context, array [I], (I + 1), array )! = False; I ++ ){}
};
// Object Traversal
VaR _ function_foreach = function (object, block, context ){
For (var key in object ){
// Only traverse local properties
If (object. hasownproperty (key) & block. Call (context, object [Key], key, object) === false ){
Break;
}
}
};
Return function (object, block, context ){
If (Object = NULL) return;
If (typeof object. Length = "Number "){
_ Array_foreach (object, block, context );
} Else {
_ Function_foreach (object, block, context );
}
};
})()

functions are not complex, but exquisite. I added some simple comments to believe that everyone can understand them.
here is an example of copy Code the code is as follows: // \ n
foreach ([, 5], function (El, index) {
If (index> 2) {
return false;
}< br> alert (index + ": "+ El);
});
function print (El, index) {
alert (index +": "+ El );
}< br> // A: A \ n B: B \ n C: C
foreach ({A: 'A', B: 'B ', c: 'C'}, print);
// 1: Stupid \ n 2: egg \ n 3: \ n 4: Seat \ n 5: right \ N 6: Ming
foreach ("stupid motto", print);
function person (name, age) {
This. name = Name | "";
This. age = age | 0;
};
person. prototype = new person;
var Fred = new person ("jxl", 22);
Fred. language = "Chinese"; // late binding
// name: jxl \ n age: 22 \ n language: Chinese
foreach (Fred, print );

Note: The index subscript In the callback function starts from 1
Why not use the built-in foreach
As with getelementsbyclassname, the built-in foreach is highly efficient, however, there are functional limitations, and it cannot exit in the middle of the loop. In our foreach, it can exit the loop by returning false in the processing function, which is more flexible.
special Length attribute
The Length attribute is a special attribute. When we see arrays, we will definitely think of length. What about the objects with the Length attribute? Then you must think of a pseudo array (class array ). What is a pseudo array? A simple understanding is to convert an array. Prototype. Slice to an object with the Length attribute of a real array. The most famous pseudo array in Javascript is the arguments object. There are many items about pseudo arrays. I will write a blog post to talk about this in the future. Remember: Do not assign the Length attribute to an object unless you know it clearly that you are going to use it as a pseudo array.
I think this function is a required function in a simple JavaScript tool library. It is the foundation of the pyramid. On the basis of it, it is encapsulated to make your library more powerful, more beautiful!

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.