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 |
function fun() { 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 global fun.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‘ function fun() { alert( this ); } fun.call( null ) // null fun.call(undefined) // undefined |
Be careful!
JavaScript adds an array of each method to iterate through a multidimensional array