Array.prototype.each = function(callback) { for (var i = 0;
Array.prototype.each = function(callback) { for (var i = 0; i < this.length; i++) callback(this[i]); } - is this ok?
我知道这是一个品味问题,但对于我的口味来说,每次我想迭代数组时使用 for 循环是不好的。所以我想出了这个:
Array.prototype.each = function(callback) {
for (var i = 0; i < this.length; i++)
callback(this[i]);
}
现在我可以做:
[10, 20, 30].each(function(n) { console.log(n/10) })
后来我在互联网上找到了一些建议这种方法的技巧,但我仍然想知道它是否没有副作用。这似乎很明显,这就是我担心的:)
我没有使用任何像jQuery或Prototype这样的库。我正在为 Node.js 编码。
我建议你使用forEach
。它在大多数现代浏览器中实现,并且填充程序由 MDN 提供。
在node.js上,它是实现的(它使用V8,与Chrome相同的引擎),所以你不需要垫片。
它的用法如下:
arr.forEach( function( el ) {
// play with "el"
} )
另外,我建议您查看 ES5 提供的新数组方法:
- 地图
- 滤波器
- 减少
- 每
- 一些
这确实有一个相当大的副作用
如果我们现在运行以下代码
a = [10, 20, 30];
for (var i in a) {
console.log(i + ": " + a[i]);
}
并检查控制台输出,我们看到:
0: 10
1: 20
2: 30
each: function (callback) {
for (var i = 0; i < this.length; i++)
callback(this[i]);
}
这将限制您可以在数组上执行的 for 循环的类型
a = [10, 20, 30];
for (var i = 0; i < a.length; ++i) {
console.log(a[i] + ": " + a[i]);
}
来自:
// Production steps of ECMA-262, Edition 5, 15.4.4.18
// Reference: http://es5.github.io/#x15.4.4.18
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback, thisArg) {
var T, k;
if (this === null) {
throw new TypeError(' this is null or not defined');
}
// 1. Let O be 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() internal
// method 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.11
if (typeof callback !== "function") {
throw new TypeError(callback + ' is not a function');
}
// 5. If thisArg was supplied, let T be thisArg; else let
// T be undefined.
if (arguments.length > 1) {
T = thisArg;
}
// 6. Let k be 0
k = 0;
// 7. Repeat, while k < len
while (k < len) {
var kValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty
// internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal
// method 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 containing kValue, k, and O.
callback.call(T, kValue, k, O);
}
// d. Increase k by 1.
k++;
}
// 8. return undefined
};
}
另外,还可以与html元素一起使用:
NodeList.prototype.forEach = HTMLCollection.prototype.forEach = Array.prototype.forEach;
瞧!
相关文章:
- 在for循环中声明i时使用var
- 为什么 for (null_object 中的 var i)进入循环体的次数超过零次
- Javascript-在for循环上从var中的对象获取适当性
- for(数组中的i)和for(var i=0;i<array.length;i++)之间的差异
- JS “for (var key in arr)” >抛出我自己的 Array.prototypes,但不是固有的.为什
- for (var x in array) 和 for (var x=0, y= array.length; x
- JavaScript 在 for 循环中返回 var
- 为什么 for(var i = 0; i < 600000000000; i++) {} 在 JavaScript 中运
- 是否可以使用 for 循环进行 DRY 量角器测试?*VaR 即将出现未定义*
- Array.prototype.each = function(callback) { for (var i = 0;
- Shorthand for "console.log("var: " + var)&quo
- Why for var在数组中返回一个字符串索引
- 在jquery get请求中从for循环调用var i
- Javascript:for(var i<array.length)v.s.for(数组中的e)
- 为什么for(arguments中的var i)没有'不起作用
- Javascript:为什么我需要在for循环中声明var I=0
- W3c验证器和for (var i=0;i
- 为什么我的javascript for循环将不工作?For (var i=1;i< 5;i++;){文档.write (
- 我如何定义一个var作为对象从对象在for循环
- 我混淆了javascript let和var在for循环