【问题标题】:logging javascript object properties [duplicate]记录javascript对象属性[重复]
【发布时间】:2017-08-12 07:41:51
【问题描述】:

请分享您对以下输出的想法。

var a = '';
console.log(a); //this logs nothing as expected.
a = 'triven';

var a = {};
console.log(a); // How javascript is aware of property here that is defined below. 
a.name = 'triven';

【问题讨论】:

  • 将鼠标悬停在“i”上
  • 你怎么了?

标签: javascript


【解决方案1】:

我想你正在寻找console.dir()

console.log() 不会做你想做的事,因为它会打印对对象的引用,当你打开它时,它已经改变了。 console.dir 在您调用它时打印对象中的属性目录。

下面的 JSON 思路不错;你甚至可以继续解析 JSON 字符串并获得一个可浏览的对象,就像 .dir() 给你的那样:

console.log(JSON.parse(JSON.stringify(obj)));

【讨论】:

  • console.dir() 也给了我与最新 chrome 中的 console.log() 完全相同的输出。
  • console.log(JSON.parse(JSON.stringify(obj)));试过了吗?
  • 是的,这很好用。
猜你喜欢
  • 2012-10-17
  • 2011-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-20
  • 2020-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多