JavaScript foreach method

Source: Internet
Author: User

Recently read some of the HTML5 and JS aspects of the book, benefited, because see more things, but there is no how to do the meditation, slowly come, perhaps recently a little nervous. Today I share with you the Foreach method of JavaScript (in fact, a method seen in the book "HTML5 Program Design").

First, the standard format of the JavaScript foreach.

Performs the specified action for each element in the array.

array1.forEach(callbackfn[, thisArg])

Parameters

Defined

Array1

Necessary. an Array object.

Callbackfn

Necessary. a function that accepts up to three parameters. for each element in the array,ForEach invokes the callbackfn function one time.

Thisarg

Optional. The object for which the This keyword can be referenced in the callbackfn function . if Thisarg is omitted , undefined is used as the this value.

If the CALLBACKFN parameter is not a function object, a TypeError exception is thrown.

For each element in the array, theForEach method invokes the CALLBACKFN function once (in ascending index order). The callback function is not called for elements that are missing from the array.

In addition to array objects, theForEach method can be used by any object that has a length property and has a property name indexed by number.

callback function syntax

The syntax for the callback function is as follows:

function Callbackfn (value, index, array1)

You can declare a callback function with up to three parameters.

The parameters of the callback function are as follows.

Callback Parameters

Defined

Value

The value of the array element.

Index

The numeric index of the array element.

Array1

The array object that contains the element.

Modifying an Array object

The ForEach method does not directly modify the original array, but the callback function may modify it.

Well, the above is copied from the Microsoft Http://technet.microsoft.com/zh-cn/ff679980%28v=vs.85%29 page, interested to go there directly to see it. In other words, the format of the general method is:

Arrayx.foreach (function (Value,index,arrayy) {...})

But for NodeList to use the following notation.

[].foreach.call (Lists,function (Valule.index.arrayy) {...})

Why can ' t I use ForEach or map on a NodeList?

NodeList is used very much like arrays and it would is tempting to use array.prototype methods on them. This is, however, impossible.

JavaScript has a inheritance mechanism based on prototypes. Array instances inherit array methods (such as ForEach or map) because their prototype chain looks like the following:

myArray --> Array.prototype --> Object.prototype --> null(The prototype chain of an object can is obtained by calling several times object.getprototypeof)

ForEach, map and the likes are own properties of the Array.prototype object.

Unlike arrays, NodeList prototype chain looks like the following:

myNodeList --> NodeList.prototype --> Object.prototype --> null

NodeList.prototypeContains item the method, but none of the array.prototype methods, so they cannot is used on nodelists.

Instance
    1. [].foreach.call (Document.queryselectorall (' section[data-bucket] '), function (Elem, i) {
    2. localstorage[' bucket ' + i] = Elem.getattribute (' data-bucket ');
    3. });

JavaScript 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.