Javascript forEach generic looping Method

Source: Internet
Author: User
Tags exit in

Copy codeThe 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 they are exquisite. I added some simple comments to believe that everyone can understand them.
Let's look at an example.
Copy codeThe Code is as follows:
// \ N
ForEach ([1, 2, 3, 4, 5], function (el, index ){
If (index> 2 ){
Return false;
}
Alert (index + ":" + el );
});
Function print (el, index ){
Alert (index + ":" + el );
}
// 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"; // bind very late
// Name: jxl \ n age: 22 \ n language: chinese
ForEach (fred, print );

Note: The index parameter subscript In the callback function starts from 1.
Why not use the built-in forEach
Like getElementsByClassName, the built-in forEach is very efficient, but it has some functional limitations and cannot exit in 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 very 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 necessary function in a simple javascript tool library. It is the foundation of the pyramid and further encapsulates it to make your library more powerful and 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.