为什么nodejs控制台。记录同一对象的不同输出格式?

时间:2021-03-15 16:56:09

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.

在上面的代码中,我为构造函数F定义了类字段。当我在节点不同的参数列表中console.log()时,F的输出结果是不同的。这是为什么呢?但是在浏览器控制台的输出是相同的。我的节点版本是v4.2.6和linux。

Thanks in advance.

提前谢谢。

1 个解决方案

#1


2  

It might be a bug. There's no good reason to differ.

它可能是一个bug。没有充分的理由对此表示异议。

Why does this happen? console.log delegates to util.format (quite literally), and format distin­guishes between a string for the first argument (that might be a format string) and something else. You can find the exact algorithm here. Basically:

这为什么会发生?控制台。日志代表跑龙套。格式(毫不夸张地说),格式迪斯汀­guishes之间的第一个参数的字符串(这可能是一个格式字符串)和其他东西。你可以在这里找到精确的算法。基本上:

  • when the first argument is a string, placeholders get replaced by the respective values, then further arguments are adjoined. Those are inspected when they are objects or symbols, but just cast to strings and concatenated otherwise.
  • 当第一个参数是字符串时,占位符会被相应的值所取代,然后进一步的参数会被附加。当它们是对象或符号时,会对它们进行检查,但只是将它们转换为字符串并将它们连接起来。
  • when the first argument is not a string, every value is inspected, then they are concatenated together.
  • 当第一个参数不是字符串时,将检查每个值,然后将它们连接在一起。

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")).

由于对象检查依赖于typeof,所以它不认为函数是对象,并且您的函数是直接绑定的。对于其他值(例如控制台),还可以观察到强制转换和检查之间的差异。日志(“0”、“示例”)和控制台。“示例”),日志(0)。

#1


2  

It might be a bug. There's no good reason to differ.

它可能是一个bug。没有充分的理由对此表示异议。

Why does this happen? console.log delegates to util.format (quite literally), and format distin­guishes between a string for the first argument (that might be a format string) and something else. You can find the exact algorithm here. Basically:

这为什么会发生?控制台。日志代表跑龙套。格式(毫不夸张地说),格式迪斯汀­guishes之间的第一个参数的字符串(这可能是一个格式字符串)和其他东西。你可以在这里找到精确的算法。基本上:

  • when the first argument is a string, placeholders get replaced by the respective values, then further arguments are adjoined. Those are inspected when they are objects or symbols, but just cast to strings and concatenated otherwise.
  • 当第一个参数是字符串时,占位符会被相应的值所取代,然后进一步的参数会被附加。当它们是对象或符号时,会对它们进行检查,但只是将它们转换为字符串并将它们连接起来。
  • when the first argument is not a string, every value is inspected, then they are concatenated together.
  • 当第一个参数不是字符串时,将检查每个值,然后将它们连接在一起。

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")).

由于对象检查依赖于typeof,所以它不认为函数是对象,并且您的函数是直接绑定的。对于其他值(例如控制台),还可以观察到强制转换和检查之间的差异。日志(“0”、“示例”)和控制台。“示例”),日志(0)。