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?

本文关键字:var for callback prototype each function Array      更新时间:2023-09-26

我知道这是一个品味问题,但对于我的口味来说,每次我想迭代数组时使用 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;

瞧!