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.prototype
Contains item
the method, but none of the array.prototype methods, so they cannot is used on nodelists.
Instance
- [].foreach.call (Document.queryselectorall (' section[data-bucket] '), function (Elem, i) {
- localstorage[' bucket ' + i] = Elem.getattribute (' data-bucket ');
- });
JavaScript foreach method