2013-03-26 173 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'