Reputation: 2308
For the following code:
function F() {
}
// Define class fields for F
F.value = [ 1, 2, 3, 4 ];
console.log('F', F); // F function F() { }
console.log(F); // { [Function: F] value: [ 1, 2, 3, 4 ] }
In the code above, I have define class fields for the constructor F
. When I console.log()
in node
different argument list, the printing result is different for F
.
The one is function F() { }
, the other is { [Function: F] value: [ 1, 2, 3, 4 ] }
. So that's why?
But the output is same in browser console.
My node
version is v4.2.6
and linux
.
Thanks in advance.
Upvotes: 2
Views: 122
Reputation: 664217
It might be a bug. There's no good reason to differ.
Why does this happen? console.log
delegates to util.format
(quite literally), and format
distinguishes between a string for the first argument (that might be a format string) and something else. You can find the exact algorithm here. Basically:
Due to the object check relying on typeof
, it does not consider functions to be objects, and your function is directly stringified. This difference between casting and inspecting can also be observed for other values (e.g. console.log("0", "example")
vs console.log(0, "example")
).
Upvotes: 2