Method of the array (Array.prototype.forEach () method)

Source: Internet
Author: User

forEach()Method executes the supplied function once for each element of the array.

Note: no new array is returned and no return value !

Application scenario: Bind event handlers for some of the same elements!

Const ARR = [' A ', ' B ', ' C '];arr.foreach (Element) {    = = Console.log (Element) );
Grammar

callbackThe function that is executed for each element in an array that receives three parameters:

    • CurrentValue (current value): The current element being processed in the array.
    • Index (index): The index of the current element being processed in the array.
    • Array: The ForEach () method that is manipulating the arrays.
    • thisArg Optional Optional parameter: The 用作 this的 value (Reference object) when the callback function is executed.
return Value: undefined.

    forEachMethod executes the callback function once for each item in the array that has a valid value in ascending order, those that have been deleted (using delete methods, and so on), or uninitialized items that are skipped (but do not include those valuesundefined 的项)(例如在稀疏数组上)。

If the 给forEach传递了thisArg argument is called, it will be passed to the callback function as its this value. Otherwise, it will be undefined passed in as its this value. The callback function can eventually observe this value, depending on this the common rules observed by the function.

   foreach   Span style= "Background-color: #ffff00;" > The scope of the traversal in First call   Callback   before OK . Call ForEach   post Add to in the array is not   callback   access to . If is is passed to   callback   is   ForEach   traverses to their moment value. Deleted items are not traversed to.

   forEach()Executes the callback function for each array element; Unlike map() or reduce() , it always returns undefined a value and is not chained. A typical use case is to perform side effects at the end of a chain.

Using Thisarg
      functionCounter () { This. Sum = 0;  This. Count = 0; } Counter.prototype.add=function(array) {Array.foreach (entry)={Console.log ( This);//Counter constructor function               This. sum + =entry; ++ This. Count; },  This);      }; varobj =NewCounter (); Obj.add ([1, 3, 5, 7]); Console.log (Obj.count); //4 = = = (1+1+1+1)Console.log (obj.sum);//+ = = (1+3+5+7)

Note: If you pass in a function argument using an arrow function expression, the thisArg argument is ignored because the arrow function is bound to the lexical this value.

Compatibility with legacy environments

if(!Array.prototype.forEach) {Array.prototype.forEach=function(callback, Thisarg) {varT, K; if( This==NULL) {      Throw NewTypeError (' This was 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") {      Throw NewTypeError (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 0K = 0; //7. Repeat, while K < Len     while(K <Len) {      varKvalue; //A. Let Pk is ToString (k).      //This was implicit for LHS operands of the 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 "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  };}

Method of the array (Array.prototype.forEach () method)

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.