Javascript Node.js中非字符串后未格式化的字符串
以下是Node.js中的错误还是功能?如果是功能,请在规格中指出 当我们称之为:Javascript Node.js中非字符串后未格式化的字符串,javascript,node.js,Javascript,Node.js,以下是Node.js中的错误还是功能?如果是功能,请在规格中指出 当我们称之为: console.log('one\ntwo', 'three\nfour'); 我们得到了预期的结果: one two three four console.log(1, 'one\ntwo', 'three\nfour'); 但如果在前面使用非字符串值,则字符串的格式将不再符合预期: one two three four console.log(1, 'one\ntwo', 'three\nfour');
console.log('one\ntwo', 'three\nfour');
我们得到了预期的结果:
one
two three
four
console.log(1, 'one\ntwo', 'three\nfour');
但如果在前面使用非字符串值,则字符串的格式将不再符合预期:
one
two three
four
console.log(1, 'one\ntwo', 'three\nfour');
产出:
1 'one\ntwo' 'three\nfour'
为什么呢
更新
通过@MuliYulzary,似乎应该根据第一个参数是否为字符串来设置格式
我发现,当第一个参数是字符串时,Node.js使用util.format(parameters)
,当第一个参数不是字符串时,它使用util.inspect
这就是它的工作原理。从文档中可以看出,它工作正常
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.
请参见文档中的示例用法,它工作正常
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.
请参见示例用法看看这个。基本上,您正在运行另一个具有不同用途的console.log变体。没有关于
控制台的规范。log
@Bergi当然有:@vitaly-t:这是关于Firefox内置控制台的MDN文档,不是规范。或者你指的是来自MDN的链接吗?看看这个。基本上,您正在运行另一个具有不同用途的console.log变体。没有关于控制台的规范。log
@Bergi当然有:@vitaly-t:这是涉及Firefox内置控制台的MDN文档,不是规范。或者你是指链接自MDN的吗?