Javascript console.log(object) 与连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14597246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript console.log(object) vs. concatenating string
提问by John Zwinck
I'm running this in node.js:
我在 node.js 中运行它:
> x = { 'foo' : 'bar' }
{ foo: 'bar' }
> console.log(x)
{ foo: 'bar' }
undefined
> console.log("hmm: " + x)
hmm: [object Object]
undefined
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' }?
我不明白的是为什么console.log(x)“漂亮地打印”对象,而字符串连接“丑陋地打印”它。更重要的是,让它打印的最佳方式是什么hmm: { foo: 'bar' }?
回答by Explosion Pills
The + xcoerces the object xinto a string, which is just [object Object]:
将+ x对象强制x转换为字符串,它只是[object Object]:
The pretty printing is a very nice and probably very complex underlying code that someone implemented as part of the consoleobject and the logmethod.
漂亮的打印是一个非常好的并且可能非常复杂的底层代码,有人将其作为console对象和log方法的一部分来实现。
Try this:
尝试这个:
console.log("hmm: ", x);
回答by Aaren Cordova
The console.log function
console.log 函数
'console.log' is an overloaded function that accepts a list of parameters that are either passed by copy (string|number|boolean) or by reference (everything else).
'console.log' 是一个重载函数,它接受通过复制(字符串|数字|布尔值)或通过引用(其他所有方式)传递的参数列表。
In the case of values passed by copy, the value is printed by casting it as a string.
In the case of values passed by reference, the value is pretty printed as the browser sees fit.
在通过复制传递值的情况下,该值通过将其转换为字符串来打印。
在通过引用传递值的情况下,该值会按照浏览器认为合适的方式打印出来。
The + operator
+ 运算符
The plus sign operator (+) is overloaded. When both sides of the operator are numbers, the sum of the two operators is returned.
加号运算符 (+) 已重载。当运算符两边都是数字时,返回两个运算符之和。
If either side of the operator is a string, then both sides will be cast as string and the concatenation of those two strings will be returned.
如果运算符的任一侧是字符串,则两侧都将转换为字符串,并返回这两个字符串的连接。
console.log("hmm: " + x);
is the same as writing
和写作一样
console.log(String("hmm: ") + String(x));
Solution
解决方案
Prevent the implicit string casting by swapping the plus sign (+) with a comma (,)
通过用逗号 (,) 交换加号 (+) 来防止隐式字符串转换
console.log("hmm: ", x);
More Info
更多信息
For a more in depth description of the 'console.log' function, see:
https://developer.mozilla.org/en-US/docs/DOM/console.log
有关“console.log”功能的更深入描述,请参阅:https:
//developer.mozilla.org/en-US/docs/DOM/console.log
For a more in depth description on the plus sign operator (+), see:
http://www.w3schools.com/js/js_operators.asp
有关加号运算符 (+) 的更深入说明,请参阅:http:
//www.w3schools.com/js/js_operators.asp
回答by Ranganadh Paramkusam
Use JSON.stringifywhen printing an object with string appending
使用JSON.stringify印刷用绳子追加对象时
console.log("Haa"+JSON.stringify(x))
回答by JP Richardson
You have multiple options:
您有多种选择:
process.stdout.write('hmm: ')
console.dir(x)
Another...
其他...
var util = require('util')
process.stdout.write('hmm: ')
console.log(util.inspect(x, true, 10, true))
See util.inspectdocs for more info.
有关更多信息,请参阅util.inspect文档。
Edit:Sorry, my mind thought I read Node.js. This is valid for Node.js only. So, I'll leave it for any Googlers.
编辑:抱歉,我以为我读了 Node.js。这仅对 Node.js 有效。所以,我会把它留给任何谷歌员工。
Edit2:I'm not crazy, I just need sleep. You did write Node.js. I'm going to add it as a tag.
Edit2:我没疯,我只是需要睡觉。你确实写了 Node.js。我要把它作为标签添加。
回答by user11737484
you can use console.log(JSON.stringify(yourObject));to print your object. it work!
你可以console.log(JSON.stringify(yourObject));用来打印你的对象。这行得通!

