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
. Under node v0.10.19 you do not get an error; you simply see this:This is because the callback to
forEach
is a three-parameter function taking the value, the index, and the array itself. The functionconsole.log
sees those three parameters and dutifully logs them.Under the Chrome browser console, however, you get
and in this case,
bind
will work:but there is an alternative way: note that the second parameter to
forEach
takes the value ofthis
to use in the callback:which works in the Chrome console and node for me. Of course, I'm sure what you want is just the values, so I'm afraid the best solution is, indeed:
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. But the varying behavior, and the fact that you have to be aware of whether your callback usesthis
is important and means we have to fall back to direct coding, even if it is verbose thanks to the keywordfunction
.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.
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. In the second example you pass in e and then log it.
This works:
Actually as @SLaks pointed out, console.log seems to be using
this
internally and when it's passed as a parameterthis
now refers to the array instance.The workaround for that is simply:
var c = console.log.bind(console);
[1,2,3,4,5].forEach(c);