First look at the implementation of the foreach
//Production steps of ECMA-262, Edition 5, 15.4.4.18
//reference:http://es5.github.io/#x15.4.4.18
if(! Array.prototype.forEach) {
Array.prototype.forEach =function(Callback, Thisarg) {
varT, K;
if( This===NULL) {
ThrowNewTypeError (' This is a null or not defined ');
}
//1. Let O is the result of calling Toobject () passing the
//|this| value as the argument.
varO = Object ( This);
//2. Let Lenvalue be the result of calling the Get () internal
//method of O with the argument "length".
//3. Let Len be ToUint32 (lenvalue).
varLen = o.length >>> 0;
//4. If Iscallable (callback) is false, throw a TypeError exception.
//see:http://es5.github.com/#x9.
if(typeofCallback!=="function") {
ThrowNewTypeError (callback +' is not a function ');
}
//5. If Thisarg is supplied, let T is thisarg; else Let
//T be undefined.
if(Arguments.length > 1) {
T = Thisarg;
}
//6. Let K is 0
k = 0;
//7. Repeat, while K < Len
while(K < Len) {
varKvalue;
//A. Let Pk be ToString (k).
//This is implicit-LHS operands of the in operator
//b. Let kpresent be the result of calling the Hasproperty
//Internal method of O with argument Pk.
//This step can is combined with C
//C. If Kpresent is true and then
if(kinchO) {
//I. Let Kvalue be the result of calling the Get internal
//method of O with argument Pk.
Kvalue = O[k];
//II. Call the call internal method of callback with T as
//The This value and argument list containing Kvalue, K, and O.
Callback.call (T, Kvalue, K, O);
}
//d. Increase K by 1.
k++;
}
//8. Return undefined
};
}
Basic usage:
Arr.foreach (callback[, Thisarg]), callback receives three parameters: CurrentValue, index, array
var ary = ["JavaScript""Java""coffeescript""TypeScript"];
Ary.foreach (function(value, index, _ary) {
":" + value);
return false;
});
Logs
0:javascript
1:java
2:coffeescript
3:typescript
Using the Some function
var ary = ["JavaScript""Java""coffeescript""TypeScript"];
Ary.some (function (value, index, _ary) {
":" + value);
return "Coffeescript";
});
Logs
0:javascript
1:java
2:coffeescript
Using the Every function
var ary = ["JavaScript""Java""coffeescript""TypeScript"];
Ary.every (function(value, index, _ary) {
":" + value);
return value.indexof ("Script") >-1;
});
Logs
0:javascript
1:java
Use fo.. Of
Let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (Let El of Arr) {
Console.log (EL);
if (el = = 5) {
break;
}
}
logs:012345
And what if foreach wanted to implement the effect of a every-like and some function?
There are several ways to get a higher ticket on the StackOverflow:
1, the use of the outside of the loop try. Catch, throw an exception when a break is required, and catch is captured;
2, rewrite the foreach (also the first method of reference);
var Breakexception = {};
Try {
[1, 2, 3].foreach (function(EL) {
Console.log (EL);
if Throw Breakexception;
});
Catch (e) {
if throw E;
}
//Use a closure to prevent the global namespace from being polluted.
(function() {
//Define stopiteration as part of the global scope if it
//isn ' t already defined.
if(typeofStopiteration = ="undefined") {
Stopiteration =NewError ("Stopiteration");
}
//The original version of Array.prototype.forEach.
varOldforeach = Array.prototype.forEach;
//If foreach actually exists, define foreach so can
//Break out of the it by throwing stopiteration. allow
//Other errors would be thrown as normal.
if(Oldforeach) {
Array.prototype.forEach =function() {
Try{
Oldforeach.apply ( This, [].slice.call (arguments, 0));
}
Catch(e) {
if(e!== stopiteration) {
ThrowE
}
}
};
}
})();
//Show the contents until you get to "2".
[0,1,2,3,4].foreach (function(Val) {
if(val = = 2)
ThrowStopiteration;
Alert (val);
});
Reference Links:
http://dean.edwards.name/weblog/2006/07/enum/
http://www.jsnoob.com/2013/11/26/how-to-break-the-foreach/
Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
How to interrupt a foreach loop in JavaScript