Array.prototype.forEach = function (action) {
for (var i = 0; i < this.length; i++) {
Action (this[i]);
}
};
/**
* Iterate objects in the same way as jqery objects
* @param {} Action
* @returns {}
*/
Array.prototype.forEachJq = function (action) {
for (var i = 0; i < this.length; i++) {
Action ($ (this[i]));
}
};
-----------------------------------------------------------------
Production steps of ECMA-262, Edition 5, 15.4.4.18reference:http://es5.github.io/#x15.4.4.18If(! Array. prototype. ForEach){Array. prototype. ForEach=function(callback, Thisarg){var TK;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.var O=Object(This);2. Let Lenvalue be the result of calling the Get () internalMethod of O with the argument "length".3. Let Len be ToUint32 (lenvalue).var len= O. length>>>0;4. If iscallable (callback) is false, throw a TypeError Exception.see:http://es5.github.com/#x9.11If(typeof Callback!=="function"){ThrowNewTypeError(callback+' is not a function ');}5. If Thisarg is supplied, let T is thisarg; else letT be Undefined.If(arguments. length>1){T= Thisarg;}6. Let K is 0 K=0;7. Repeat, while K < LenWhile(k< Len){var kvalue;A. Let Pk be ToString (k).This was implicit for LHS operands of the operatorB. Let Kpresent be the result of calling the HaspropertyInternal method of O with argument Pk.This step can is combined with CC. If Kpresent is true and thenIf(kIn O){I. Let Kvalue be the result of calling the Get internalMethod 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 Contai Ning kvalue, k, and O. callback. Call //d. increase K by 1. k++ //8. return undefined };}
JavaScript implements foreach for arrays