forEach
来源:互联网 发布:手机库存盘点软件 编辑:程序博客网 时间:2024/05/16 09:06
Executes a provided function once per array element. If a The range of elements processed by The following code prints a line for each element in an array: The following code creates a simple writer object and then uses the https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach
Summary
Syntax
array.forEach(callback[, thisObject]);
Parameters
callback
thisObject
this
when executing callback
.Description
forEach
executes the provided function (callback
) once for each element present in the array. callback
is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.callback
is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.thisObject
parameter is provided to forEach
, it will be used as the this
for each invocation of the callback
. If it is not provided, or is null
, the global object associated with callback
is used instead.forEach
does not mutate the array on which it is called.forEach
is set before the first invocation of callback
. Elements which are appended to the array after the call to forEach
begins will not be visited bycallback
. If existing elements of the array are changed, or deleted, their value as passed to callback
will be the value at the time forEach
visits them; elements that are deleted are not visited.Compatibility
forEach
is a recent addition to the ECMA-262 standard; as such it may not be present in other implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of forEach
in implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming Object
and TypeError
have their original values and that fun.call
evaluates to the original value of Function.prototype.call
.if (!Array.prototype.forEach)
{
Array.prototype.forEach = function(fun /*, thisp */)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
fun.call(thisp, t[i], i, t);
}
};
}Examples
Example: Printing the contents of an array
function printElt(element, index, array) {
print("[" + index + "] is " + element); // assumes print is already defined
}
[2, 5, 9].forEach(printElt);
// Prints:
// [0] is 2
// [1] is 5
// [2] is 9Example: Printing the contents of an array with an object method
writeln
method to write one line per element in the array:var writer = {
sb: [],
write: function (s) {
this.sb.push(s);
},
writeln: function (s) {
this.write(s + "/n");
},
toString: function () {
return this.sb.join("");
}
};
[2, 5, 9].forEach(writer.writeln, writer);
print(writer.toString()); // assumes print is already defined
// Prints:
// 2
// 5
// 9
- foreach
- foreach
- foreach
- foreach
- Foreach
- forEach
- foreach
- foreach
- foreach
- forEach
- foreach
- foreach
- foreach
- foreach
- foreach
- foreach
- foreach
- foreach
- Ajax + JSON
- NRF24l01无线温度传感 收发程序
- 2.Singleton(创建型模式)
- ORA-01704: string literal too long 和 ORA-00932: inconsistent datatypes: expected NUMBER got LONG
- 最经典 VC游戏开发全过程
- forEach
- Eclipse bookmark 使用方法
- 公司服务器瘦身/var占用空间
- 用Lisp解释Lisp
- shell 语言学习
- AJAX+Servlet+Myeclipse8.5使用Get传输
- Building on Quicksand - Pat Helland, Dave Campbell 阅读笔记
- ASF1.0
- sql 计算星期几