2017-08-12 41 views
0

请分享有关下方输出你的看法。记录JavaScript对象的属性

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

first log

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

enter image description here

+3

将鼠标放在“我” – Ryan

+0

什么让你烦恼? –

回答

1

我认为你正在寻找console.dir()

console.log()不会做你想做的,因为它打印到对象的引用,并通过你的流行打开的时候,它的改变。 console.dir在您调用对象时打印对象中的属性目录。下面

的JSON的想法是好的;你甚至可以去解析JSON字符串,并获得可浏览的对象喜欢什么.DIR()会给你:

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

+0

console.dir()也给我完全相同输出的console.log()在最新的Chrome。 – Triven

+0

的console.log(JSON.parse(JSON.stringify(OBJ)));试过这个? – PSN

+0

是的,这工作正常。 – Triven