JavaScript implements a foreach array

Source: Internet
Author: User

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

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.