going back to writing javascript for the time being, i wonder why this isn't possible
[array].forEach(console.log)
going back to writing javascript for the time being, i wonder why this isn't possible
[array].forEach(console.log)
Because console.log.bind(console)
, otherwise the .log()
method of a console
object is invoked with a wrong context.
The general rule to follow is: the method must be called with a proper context unless it's stated otherwise.
So in this particular case no one states it can be invoked with say undefined
, so you should not expect it to work. And even if it does - you should not rely on it.
It will work correctly in certain browsers and not in others.
A major point of confusion in Javascript is the way this
behaves. Often these will produce vastly different behavior:
myObj.method();
var a = myObj.method;
a();
What you are doing there is passing console.log
as a function into forEach
, which is separating it from the object it is attached to. This will cause any uses of this
inside the log
method to refer to the wrong thing, and quite possibly cause the method to not work correctly.
To remedy this, you can pass a thisArg
into .forEach
:
[array].forEach(console.log, console);
or use .bind()
:
[array].forEach(console.log.bind(console));
You can, if the browser supports it, although even then you shouldn't rely on it.
The reason it doesn't work on some browsers is that passing a function reference like this (even if that function is a property of an object such as console
) doesn't set the this
context correctly within that function.
When the (browser specific) implementation of the log
function tries to access its this
variable it finds that instead of referring to console
it actually refers to the global object.
To fix it, you can use this:
[array].forEach(console.log.bind(console));
where the .bind
call returns a new function wrapping the original function and whose context is set to the passed parameter (e.g. console
)