我正在使用 console.log 语句进行调试,但遇到了一个场景,其中使用 ',' 或 '+' 与 console.log 语句正在以不同的模式记录输出。例如
(function() {
var x = [];
x.push({
a: 1,
b: 2,
}, {
a: 4,
b: 3,
}, {
a: 5,
b: 6
}, {
a: 7,
b: 8,
})
console.log('Logging with , ', x);
console.log('Logging with + ' + x);
}())
当我将 ',' 与 console.log 一起使用时,我看到的输出为
Logging with , [Object, Object, Object, Object]
每个对象都是可扩展的。但是使用“+”我看到输出为
Logging with + [object Object],[object Object],[object Object],[object Object]
为了演示我创建了这个jsfiddle https://jsfiddle.net/Lj5q7c50/.
您能帮我理解为什么我们会看到这种差异吗?
+
(字符串连接运算符)与对象将调用toString
对象上的方法和string将被退回。
所以,'' + object
相当于object.toString()
. And toString
对象返回时"[object Object]"
.
With ,
该对象作为单独的参数传递给log https://developer.mozilla.org/en-US/docs/Web/API/Console/log method.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)