【问题标题】:Array.prototype.includes on Node js versions <= 4Node js 版本 <= 4 上的 Array.prototype.includes
【发布时间】:2018-01-06 04:53:16
【问题描述】:

我写了一个 Gruntfile,它大量使用了 Array.prototype.includes() 和类似的函数。我发现我需要将 node 的版本降级到版本 4.4.5。一旦我这样做,我将不再能够使用诸如if ( myarray.includes(somevalue) )之类的语句,并且它会失败说:&gt;&gt; TypeError: myarray.includes is not a function. 当我查看节点文档时,它似乎是针对当前版本的节点,所以我'我不确定有什么替代方案。

在节点 4 及以下版本中,数组“包含”的等价物是什么?另外,还有其他我需要注意的巨大差异吗? (我发现的另一个是函数声明中不支持默认参数)。

【问题讨论】:

    标签: javascript arrays node.js gruntjs node-modules


    【解决方案1】:

    你总是可以只填充 includes 以便你可以继续使用它。甚至还有一个“官方”的 polyfill here

    无论如何,除此之外,等效的方法是 indexOf 方法,如果未找到该项目,则返回 -1,否则返回其索引。所以

    array.includes(item);
    

    可以替换为

    array.indexOf(item) !== -1;
    

    【讨论】:

    • 这些都是很好的答案,但我想我喜欢这个答案,因为它是我最初所追求的(在较低版本的节点中等效)。公平地说 - polyfill 将使它成为一个无缝过渡,所以我喜欢这样,但在我的情况下,这会使代码对不熟悉 js 的其他人更加困惑,所以我会坚持这种方法。谢谢大家!
    • 它不适用于["A", , , "D"].indexOf(undefined) !== -1 //--&gt; false
    【解决方案2】:

    处理这种情况的最佳方法是添加一个 polyfill,让您无需修改​​即可运行代码,因为修改可能会导致错误。 The polyfill you are looking for can be found here。要使用它,您需要在尝试使用 .includes 之前运行此代码,通常是在您的应用程序启动的任何地方。

    // https://tc39.github.io/ecma262/#sec-array.prototype.includes
    if (!Array.prototype.includes) {
      Object.defineProperty(Array.prototype, 'includes', {
        value: function(searchElement, fromIndex) {
    
          if (this == null) {
            throw new TypeError('"this" is null or not defined');
          }
    
          // 1. Let O be ? ToObject(this value).
          var o = Object(this);
    
          // 2. Let len be ? ToLength(? Get(O, "length")).
          var len = o.length >>> 0;
    
          // 3. If len is 0, return false.
          if (len === 0) {
            return false;
          }
    
          // 4. Let n be ? ToInteger(fromIndex).
          //    (If fromIndex is undefined, this step produces the value 0.)
          var n = fromIndex | 0;
    
          // 5. If n ≥ 0, then
          //  a. Let k be n.
          // 6. Else n < 0,
          //  a. Let k be len + n.
          //  b. If k < 0, let k be 0.
          var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
    
          function sameValueZero(x, y) {
            return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
          }
    
          // 7. Repeat, while k < len
          while (k < len) {
            // a. Let elementK be the result of ? Get(O, ! ToString(k)).
            // b. If SameValueZero(searchElement, elementK) is true, return true.
            if (sameValueZero(o[k], searchElement)) {
              return true;
            }
            // c. Increase k by 1. 
            k++;
          }
    
          // 8. Return false
          return false;
        }
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-09
      • 2022-08-12
      • 2022-07-31
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-31
      • 1970-01-01
      相关资源
      最近更新 更多