为什么forEach(console.log)不起作用?

时间:2016-11-26 20:49:37

标签: javascript function logging anonymous-function

在JavaScript中,为什么.forEach(console.log)仅在您将console.log包装到另一个函数中时为您提供所期望的输出?

a = [0, 1, 2, 3, 4, 5, 6]
[ 0, 1, 2, 3, 4, 5, 6 ]
a.forEach(console.log)
0 0 [ 0, 1, 2, 3, 4, 5, 6 ]
1 1 [ 0, 1, 2, 3, 4, 5, 6 ]
2 2 [ 0, 1, 2, 3, 4, 5, 6 ]
3 3 [ 0, 1, 2, 3, 4, 5, 6 ]
4 4 [ 0, 1, 2, 3, 4, 5, 6 ]
5 5 [ 0, 1, 2, 3, 4, 5, 6 ]
6 6 [ 0, 1, 2, 3, 4, 5, 6 ]
undefined
a.forEach(function(n) { console.log(n) })
0
1
2
3
4
5
6
undefined

导致这种输出差异的原因是什么?

0 个答案:

没有答案
相关问题