是node.js中的以下错误还是一个功能?如果是一个功能,请指出规格
当我们调用此问题时:
console.log('onentwo', 'threenfour');
我们得到了预期:
one
two three
four
但是,如果我们在其前面使用非弦乐值,则不再按预期的是字符串格式:
console.log(1, 'onentwo', 'threenfour');
输出:
1 'onentwo' 'threenfour'
为什么?
update
从@muliyulzary的链接中,似乎应该根据第一个参数是字符串来设置格式。
我发现,当第一个参数是字符串时,node.js使用 util.format(parameters)
,当第一个参数不是字符串时,它使用 util.inspect
。
这就是它的工作方式。
来自文档console.log,它的工作正常
console.log(object [, object, ...])
Logs a debug level message. You pass one or more objects to this method, each of which are evaluated and concatenated into a space-delimited string. The first parameter you pass to console.log() may contain Format Specifiers.
请参阅此处的示例用法