Javascript console.log(对象)与串联字符串 [英] Javascript console.log(object) vs. concatenating string
本文介绍了Javascript console.log(对象)与串联字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在node.js中运行它:
I'm running this in node.js:
> x = { 'foo' : 'bar' }
{ foo: 'bar' }
> console.log(x)
{ foo: 'bar' }
undefined
> console.log("hmm: " + x)
hmm: [object Object]
undefined
我不明白的是为什么 console.log(x)
漂亮地打印对象,而字符串连接丑陋地打印它。更重要的是,打印的最佳方法是什么 hmm:{foo:'bar'}
?
What I don't understand is why console.log(x)
"pretty-prints" the object, whereas string concatenation "ugly-prints" it. And more importantly, what's the best way to make it print hmm: { foo: 'bar' }
?
推荐答案
+ x
将对象 x
强制转换为字符串,这只是 [object Object]
:
The + x
coerces the object x
into a string, which is just [object Object]
:
漂亮的打印是一个非常好的,可能非常复杂的底层代码,有人实现作为控制台
对象和日志
方法的一部分。
The pretty printing is a very nice and probably very complex underlying code that someone implemented as part of the console
object and the log
method.
试试这个:
console.log("hmm: ", x);
这篇关于Javascript console.log(对象)与串联字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文