2017-08-12 88 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