【发布时间】: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