为什么非法调用console.log作为函数参数? [英] Why is console.log illegaly invocated as a function parameter?
问题描述
当我尝试时
[1,2,3] .forEach(alert);
它会按照预期为数组中的每一项打开消息框。
但是,当我尝试
[1,2,3] .forEach(console.log);
我收到以下错误
Uncaught TypeError:非法调用
为什么?
我个人得到无效的调用对象
。
请参阅 尝试 When I try it opens message box for every item of the array as expected. But when I try I receive following error Why? Personally I get See, Try 这篇关于为什么非法调用console.log作为函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! [1,2,3] .forEach(console.log)
本质上是对数组进行迭代的简写方式,对于运行 console.log.call(theArray,theItem)
的每个项目。然而, console.log
要求这个
是一个类型为 Console $ c的对象
$ b [1,2,3] .forEach(function(i){console.log(i );})
[1,2,3].forEach(alert);
[1,2,3].forEach(console.log);
Uncaught TypeError: Illegal invocation
Invalid calling object
.[1,2,3].forEach(console.log)
is essentially a shorthand way of iterating over the array and for each item running console.log.call(theArray,theItem)
. However, console.log
requires that this
be an object of type Console
, thus the error.[1,2,3].forEach(function(i) {console.log(i);})