2013-03-26 175 views
-6

我已經把我的問題,下面一起在代碼中的註釋裏:對象和屬性在JavaScript

var box = {}; 

    box.content = box; // box{ 'content': {} } right? 



    show('content' in box); // true because content exist inside of the box object 

    show('content' in box.content); // false because box.content contains an empty object! Right? 
+1

咦?這並沒有太大的意義,並且可以通過自己做一些測試來輕鬆地回答... – BenM 2013-03-26 16:57:45

+0

'ReferenceError:show is not defined' – ShuklaSannidhya 2013-03-26 16:59:11

+2

''''''''content''的無盡鏈條 - - >'box' - >'content' - >'hell' ... – ShuklaSannidhya 2013-03-26 17:02:01

回答

2

是的,你的說法都正確。

box.content = box; 

這是結果在控制檯:

因爲你在這一行建立遞歸

console.log('content' in box); // true 
console.log('content' in box.content); // true 

第二返回true: 其實第二個也應該返回true

enter image description here

+0

我的控制檯不同意 – Bruno 2013-03-26 16:59:30

+0

是的,我的也沒有。 show()函數是爲我的控制檯定義的,它爲第二個show()生成true。 – DEdesigns57 2013-03-26 17:01:36

+0

我不明白爲什麼它爲第二個輸出返回true。 – DEdesigns57 2013-03-26 17:02:48

0

這應該完全遞歸地返回true,因爲您有一個對它在'content'中的對象的引用RTY。

console.log(blah.content.content.content.content === blah) //shows 'true'