【问题标题】:Interpreting Array.forEach.apply results解释 Array.forEach.apply 结果
【发布时间】:2018-02-24 19:43:12
【问题描述】:

在我的浏览器控制台中,我输入了以下内容:

Array.forEach.apply(null,['a','b','c'])

回复是:

Uncaught TypeError: b is not a function
at String.forEach (<anonymous>)
at <anonymous>

我不太确定我认为会发生什么,但不是这样。另外:

Array.prototype.forEach.apply(null,['a','b','c']);
Uncaught TypeError: Array.prototype.forEach called on null or undefined
at forEach (<anonymous>)
at <anonymous>

我觉得理解这一点将有助于扩展我的 JS 智慧。有没有 sage JS 开发者知道这个?

另外:

['a','b','c'].forEach.apply((function(a,b){return b;}),['a','b','c'])

回复是:

Uncaught TypeError: a is not a function
at String.forEach (<anonymous>)
at <anonymous>

【问题讨论】:

  • 为什么你首先在阵列上使用.forEach.apply而不是.forEach()
  • 您是否阅读了apply 上的文档?第一个参数必须是 this 对象,即对于数组方法,这应该是应用该方法的数组。

标签: javascript arrays foreach


【解决方案1】:

Look forEach 应该作为参数接收。

arr.forEach(function callback(currentValue[, index[, array]]) {

然后看看你用apply传递了什么:

Array.forEach.apply(null,['a','b','c'])

所以:

  • this 值(arr,应该是一个数组)是null
  • 第一个参数(callback,应该是一个函数)是字符串a'
  • 第二个和第三个参数(根本不应该存在)也是字符串

它完全失败是完全可以预料的。没有什么是正确的数据。

['a','b','c'].forEach.apply((function(a,b){return b;}),['a','b','c'])

这次:

  • this 值(应该是一个数组)是一个函数
  • 而且论点和上次有同样的问题

你还没有说出你想要达到的目标,但你可能正在寻找这样的东西:

function log_current_value (current_value) {
    console.log(current_value);
};
Array.prototype.forEach.apply(['a','b','c'], [log_current_value]);

【讨论】:

    【解决方案2】:

    使用函数apply,您需要传递一个参数数组,即:[fn]:

    ['a','b','c'], [fn] <---- Callback function within an array.
     ^               
     |__ Array (First param).
    

    function fn(e) {
      console.log(e);
    }
    
    Array.prototype.forEach.apply(['a','b','c'], [fn]);

    资源

    【讨论】:

    • 没有。 this 值需要是要循环的数组,而不是 null。作为apply 的第二个参数的数组中的第一个(也是唯一一个)项必须是函数。 您的现场演示引发错误!
    • @ScottMarcus 好吧,在 Firefox 中没有。我去看看。
    • .forEach.apply 的参数应该是 [], [],而不是 [[], ]
    • @ScottMarcus 在 Firefox 作品中很奇怪。我需要检查文档。
    【解决方案3】:

    问题是您将null 作为第一个参数传递给.apply(),而您应该传递您希望.forEach() 对其进行操作的数组。

    然后,.forEach() 要求为其提供一个回调函数,该函数将为每个数组项调用,并且 .apply() 方法还要求您在数组中传递任何其他参数.典型的 .forEach() 调用如下所示:

    [...].forEach(function(value, index, array) { ...callback code...});
    

    所以整个函数参数也需要提供给.apply()

    .apply() 应该传递给(thisArg, [ arg, arg, arg, ...]),第二个参数是正在应用的函数所需的参数数组。

    所以,下面是您所看到的内容、原因和正确语法的细分:

    // The following fails with an error of: "Cannot read property 'apply' of undefined"
    // because .forEach is a member of Array.prototype
    //Array.forEach.apply(null,['a','b','c'])
    
    // The following fails with an error of: "Array.prototype.forEach called on null or undefined"
    // because .forEach is being passed null when it should be passed the array that it needs
    // to enumerate:
    //Array.prototype.forEach.apply(null,['a','b','c'])
    
    // Pass the array to enumerate and an array of any arguments that .forEach() requires:
    Array.prototype.forEach.apply(['a','b','c'], [ function(v,i,a){ console.log(v); } ]);

    【讨论】:

      猜你喜欢
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 2017-06-13
      • 2020-07-28
      相关资源
      最近更新 更多