JavaScript foreach Universal Cyclic traversal method _javascript Tips

Source: Internet
Author: User
Tags exit in
Copy code code as follows:

var ForEach = (function () {
Traversal of arrays and pseudo arrays
var _array_foreach = function (Array, block, context) {
if (array = = null) return;
Special handling 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++) {}
};
Traversal of objects
var _function_foreach = Function (object, block, context) {
for (var key in object) {
Traverse Local Properties only
if (Object.hasownproperty (key) &&block.call (context, Object[key], key, object) ===false) {
Break
}
}
};
return function (object, blocks, context) {
if (object = = null) return;
if (typeof object.length = = "Number") {
_array_foreach (object, block, context);
}else{
_function_foreach (object, block, context);
}
};
})()

The function itself is not complex, but very ingenious. Add a few simple comments, think you can read.
Take a look at some examples
Copy Code code as follows:

1:1 \ n 2:2
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 \ b:b \ C:c
ForEach ({A: ' A ', B: ' B ', C: ' C '},print);
1: Stupid 2: Eggs \ n 3: \ n 4: Block \ n 5: Right \ n 6: Ming
ForEach ("Idiot's 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";//very late binding
NAME:JXL \ age:22 \ Language:chinese
ForEach (Fred,print);

Note: The index parameter subscript in the callback function starts at 1
Why not use a built-in foreach
As with Getelementsbyclassname, the built-in foreach is highly efficient, but has limited functionality and is unable to exit in the middle of the loop. In our foreach, it can exit the loop in a handler function by returning false, more flexible.
The Special Length property
The Length property is a very special property, and when you see an array, you must think of length, and see the object with the length attribute? Then you must think of a pseudo array (an array of classes). So what is a pseudo array? The simple understanding is that it can be converted to a true array of objects with the length attribute through Array.prototype.slice. The most famous pseudo array of JavaScript is the arguments object. There are a lot of things about pseudo arrays, and I'm going to write a blog post about this stuff later. Just remember: Don't give the object the length attribute, unless you know for sure that you're going to use it as an array of perjury.
I think this function is a simple JavaScript library of the necessary functions, it is the foundation of the pyramid, on its basis, the encapsulation, you can 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.