简体   繁体   English

将console.log作为参数传递给forEach时为什么不起作用?

[英]Why doesn't console.log work when passed as a parameter to forEach?

This is just out of curiosity, but do any of you have an idea why this code won't work? 这只是出于好奇,但是你们中的任何人都知道为什么该代码无法正常工作吗?

[1, 2, 3, 4, 5].forEach(console.log);

// Prints 'Uncaught TypeError: Illegal invocation' in Chrome

On the other hand, this seems to work fine: 另一方面,这似乎很好用:

[1, 2, 3, 4, 5].forEach(function(n) { console.log(n) });

So... ? 那...

It's worth pointing out that there is a difference in behavior in the implementation of console.log . 值得指出的是, console.log的实现在行为上有所不同。 Under node v0.10.19 you do not get an error; 在节点v0.10.19下,您不会收到错误; you simply see this: 您只是看到以下内容:

> [1,2,3,4,5].forEach(console.log);
1 0 [ 1, 2, 3, 4, 5 ]
2 1 [ 1, 2, 3, 4, 5 ]
3 2 [ 1, 2, 3, 4, 5 ]
4 3 [ 1, 2, 3, 4, 5 ]
5 4 [ 1, 2, 3, 4, 5 ]

This is because the callback to forEach is a three-parameter function taking the value, the index, and the array itself. 这是因为对forEach的回调是一个三参数函数,接受值,索引和数组本身。 The function console.log sees those three parameters and dutifully logs them. 函数console.log看到这三个参数,并认真记录它们。

Under the Chrome browser console, however, you get 但是,在Chrome浏览器控制台下,

> [1,2,3,4,5].forEach(console.log);
TypeError: Illegal invocation

and in this case, bind will work: 在这种情况下, bind 起作用:

 > [1,2,3,4,5].forEach(console.log.bind(console));
 1 0 [ 1, 2, 3, 4, 5 ]
 2 1 [ 1, 2, 3, 4, 5 ]
 3 2 [ 1, 2, 3, 4, 5 ]
 4 3 [ 1, 2, 3, 4, 5 ]
 5 4 [ 1, 2, 3, 4, 5 ]

but there is an alternative way: note that the second parameter to forEach takes the value of this to use in the callback: 但有另一种方法:注意, 到第二个参数forEach需要的值this在回调的使用方法:

> [1,2,3,4,5].forEach(console.log, console)
1 0 [ 1, 2, 3, 4, 5 ]
2 1 [ 1, 2, 3, 4, 5 ]
3 2 [ 1, 2, 3, 4, 5 ]
4 3 [ 1, 2, 3, 4, 5 ]
5 4 [ 1, 2, 3, 4, 5 ]

which works in the Chrome console and node for me. 在我的Chrome控制台和节点中均可使用。 Of course, I'm sure what you want is just the values, so I'm afraid the best solution is, indeed: 当然,我确定您想要的只是值,因此,恐怕最好的解决方案确实是:

> [1,2,3,4,5].forEach(function (e) {console.log(e)});
1
2
3
4
5

Whether node's behavior is a bug, or it simply takes advantage of the fact that console.log is not specified by ECMA is interesting in its own right. 节点的行为是错误还是仅利用ECMA未指定console.log的事实本身就很有趣。 But the varying behavior, and the fact that you have to be aware of whether your callback uses this is important and means we have to fall back to direct coding, even if it is verbose thanks to the keyword function . 但是变化的行为以及必须知道回调是否使用了回调函数this很重要,这意味着我们必须退回到直接编码,即使由于关键字function而变得冗长。

这有效:

[1,2,3,4,5].forEach(console.log.bind(console));

Actually as @SLaks pointed out, console.log seems to be using this internally and when it's passed as a parameter this now refers to the array instance. 其实作为@SLaks指出,执行console.log好像是用this内部,当它作为一个参数传递真实this现指阵列实例。

The workaround for that is simply: 解决方法很简单:

var c = console.log.bind(console); [1,2,3,4,5].forEach(c);

I can't say I've seen that syntax, but my guess is because log expects a parameter, being the message/object/etc to log in the console. 我不能说我已经看过这种语法,但是我的猜测是因为log需要一个参数,即要登录控制台的message / object / etc。

in the first example, you are just passing a function reference to forEach, which is fine if your function doesn't expect paramater which make the function behave as expected. 在第一个示例中,您只是将一个函数引用传递给forEach,如果您的函数不希望参数使函数按预期运行,这很好。 In the second example you pass in e and then log it. 在第二个示例中,您传入e,然后将其记录下来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当console.log作为参数传递时,它起作用,但是当array.push被传递参数时,它不起作用,为什么? - When console.log passed as a parameter it works, but when array.push is passed a parameter it doesn't work why? 为什么console.log()不对传递的变量进行快照? - Why doesn't console.log() take a snapshot of the passed variables? 为什么“console.log”在“return”命令后不起作用? - Why "console.log" doesn't work after a "return" command? 为什么“console.log()”在这个网站上不起作用? - Why doesn't "console.log()" work on this website? 为什么console.log不能作为参数? - Why console.log doesn't work as an argument? 为什么 conv.add 在使用 API 时 console.log 不起作用 - Why doesn't conv.add work when console.log does when using an API console.log有时不起作用 - console.log doesn't work sometimes 为什么我的 React 组件不会在 foreach 中呈现我想要的内容,但会使用 console.log 记录数组? - Why doesn't my React component renders what I want in a foreach, but will console.log the array? 为什么console.log不能在JSC环境中工作,但它可以在Safari的调试控制台中运行 - Why doesn't console.log work in the JSC environment but it works in Safari's debug console 为什么你不能做[array] .forEach(console.log) - why can't you do [array].forEach(console.log)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM