JavaScript adds an array of each method to iterate through a multidimensional array

Source: Internet
Author: User

Since the ECMA provides a way to iterate through an array, foreach () can only traverse one-dimensional arrays and does not provide a way to iterate through a multidimensional array, so we implement a each () method ourselves to traverse the multidimensional array.

<script Charset=utf-8 type=text/javascript>

/*var arr = [1,2,3,[4,[5]];

Arr.foreach (function (Item,index,arr) {

alert (item);

});

*/

Simulating the ECMA ForEach loop traversing a multidimensional array

var arr = [1,2,3,[4,[5,[6,[7]]];

Using prototypes, you can extend the properties and methods of an object

Array.prototype.each = function (fn) {

try{

Counter

this.i | | (this.i=0);

Determine the length of the array must be greater than 0 && pass in must be a function to loop through the operation

if (this.length > 0 && fn.constructor = = Function) {

while (This.i < this.length) {

Get each item of an array

var e = this[this.i];

If you have taken each item of the array && the item is still set to a recursive operation

if (e && e.constructor = = Array) {

E.each (FN);

}else{

If you take an array of each item && the item is not an array then the FN function prints each item of the group

Fn.call (e,e);//Do not understand (see below to explain or see links) http://www.cnblogs.com/snandy/archive/2012/03/01/2373243.html

}

Make I increment

this.i++;

}

Finally put I empty, garbage collection mechanism recovery

THIS.I = null;

}

}catch (ex) {

}

Returns the current object

return this;

}

The argument to the ForEach () method is a function and there are parameters inside the function, and here we set the parameter in the function as a

Arr.each (function (item) {

alert (item);

});

</script>

———————————————————— the split-line call parameter —————————————————————— the first argument to the null/undefined function is the this point to window or global

Call/apply is used to change the execution context (this) of a function, and their first parameter, Thisarg, is an object, which is the this within a function.

Most of the time you preach what is inside the function. Only with the call example

1234567 functionfun() {    alert(this);}fun.call(1);fun.call(‘a‘);fun.call(true);fun.call({name:‘jack‘});

Pop up "1", "a", "true", "[Object Object]" respectively.

There are two situations where it is important to note that when null or undefined is passed, it is the global variable of the JS execution environment. The browser is window, and other environments (such as node) are global.

12 fun.call(null); // window or globalfun.call(undefined); // window or global

This is explained in ECMAScript5.1 15.3.4.4, as follows

In strict mode, the situation is different, ES3 more tolerant as far as possible to speculate on the code intent. ES5 Strict mode (except IE6/7/8/9) is no longer speculative, any parameters passed to Call/apply are no longer converted. As follows

123456 ‘use strict‘functionfun() {    alert(this);}fun.call(null)      // nullfun.call(undefined) // undefined

Be careful!

JavaScript adds an array of each method to iterate through a multidimensional array

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.