Jan*_*roň 2 javascript google-chrome
当我尝试
[1,2,3].forEach(alert);
Run Code Online (Sandbox Code Playgroud)
它会按预期打开数组中每个项目的消息框.
但是,当我尝试
[1,2,3].forEach(console.log);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Uncaught TypeError: Illegal invocation
Run Code Online (Sandbox Code Playgroud)
为什么?
就我个人而言Invalid calling object
.
看,[1,2,3].forEach(console.log)
本质上是迭代数组和运行的每个项目的简便方法console.log.call(theArray,theItem)
.但是,console.log
要求this
是类型的对象Console
,因此错误.
尝试 [1,2,3].forEach(function(i) {console.log(i);})
归档时间: |
|
查看次数: |
155 次 |
最近记录: |