如何显示 JavaScript 对象? [英] How can I display a JavaScript object?

查看:19
本文介绍了如何显示 JavaScript 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以字符串格式显示 JavaScript 对象的内容,就像我们alert 变量一样?

How do I display the content of a JavaScript object in a string format like when we alert a variable?

我想以同样的格式显示一个对象.

The same formatted way I want to display an object.

推荐答案

如果您想打印对象以进行调试,请使用以下代码:

If you want to print the object for debugging purposes, use the code:

var obj = {
  prop1: 'prop1Value',
  prop2: 'prop2Value',
  child: {
    childProp1: 'childProp1Value',
  },
}
console.log(obj)

将显示:

注意:您必须记录对象.例如,这行不通:

Note: you must only log the object. For example, this won't work:

console.log('My object : ' + obj)

注意':也可以在log方法中使用逗号,那么输出的第一行就是字符串,之后就是对象呈现:

Note ': You can also use a comma in the log method, then the first line of the output will be the string and after that, the object will be rendered:

console.log('My object: ', obj);

这篇关于如何显示 JavaScript 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆