在console.log中打印Javascript对象时的不同行为
如果我们对一个对象执行在console.log中打印Javascript对象时的不同行为,javascript,jquery,node.js,underscore.js,Javascript,Jquery,Node.js,Underscore.js,如果我们对一个对象执行console.log(),就会看到它的属性。但是,如果console.log包含其他字符串,则只会得到[object object] 问题:为什么不打印对象的属性?如何使用console.log打印字符串和对象的属性?如果有帮助,可以使用下划线.js和节点包 > myThing = {'name': 'Doge', 'value': 1000} Object {name: "Doge", value: 1000} > console.log(myThing)
console.log()
,就会看到它的属性。但是,如果console.log
包含其他字符串,则只会得到[object object]
问题:为什么不打印对象的属性?如何使用console.log
打印字符串和对象的属性?如果有帮助,可以使用下划线.js和节点包
> myThing = {'name': 'Doge', 'value': 1000}
Object {name: "Doge", value: 1000}
> console.log(myThing)
Object {name: "Doge", value: 1000}
> console.log('Logging: ' + myThing)
Logging: [object Object]
所需输出
Logging: Object {name: "Doge", value: 1000}
{ name: 'Doge', value: 1000 }
Logging: [object Object]
Logging: { name: 'Doge', value: 1000 }
--在浏览器中
因为当您使用console.log('Logging:'+myThing)
时,它使用字符串连接,其中对象myThing
转换为字符串表示形式[object object]
你可以用
console.log('Logging: ', myThing)
或使用-在现代浏览器中(对于旧浏览器,请使用类似的库)
--在浏览器中
因为当您使用console.log('Logging:'+myThing)
时,它使用字符串连接,其中对象myThing
转换为字符串表示形式[object object]
你可以用
console.log('Logging: ', myThing)
或使用-在现代浏览器中(对于旧浏览器,请使用类似的库)
Node.js有一个名为的内置模块,您可以使用它来显示对象
var util = require("util");
myThing = {'name': 'Doge', 'value': 1000};
console.log(myThing);
console.log('Logging: ' + myThing);
console.log('Logging: ' + util.inspect(myThing));
输出
Logging: Object {name: "Doge", value: 1000}
{ name: 'Doge', value: 1000 }
Logging: [object Object]
Logging: { name: 'Doge', value: 1000 }
Node.js有一个名为的内置模块,您可以使用它来显示对象
var util = require("util");
myThing = {'name': 'Doge', 'value': 1000};
console.log(myThing);
console.log('Logging: ' + myThing);
console.log('Logging: ' + util.inspect(myThing));
输出
Logging: Object {name: "Doge", value: 1000}
{ name: 'Doge', value: 1000 }
Logging: [object Object]
Logging: { name: 'Doge', value: 1000 }
控制台
内部使用util。您可以使用它们获得任何所需的格式。@user568109如果我们util.inspect
我们可以自定义深度和颜色参数,对吗?是的,它可以处理深度和颜色。便于调试。控制台
内部使用util。您可以使用它们获得任何所需的格式。@user568109如果我们util.inspect
我们可以自定义深度和颜色参数,对吗?是的,它可以处理深度和颜色。便于调试。