在窗口对象奇怪的自我对象 [英] Weird self object in window object
问题描述
我不明白为什么窗口
正在自
和自
正在窗口
对象。
如果你去DEV-工具或萤火虫和写入窗口
你有 DOM窗口
对象自
是这样的对象下。怪异的是,窗口
正在自
又来了!
您可以写
<$p$p><$c$c>window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self和你仍然获得窗口
对象!
怎么样?!
在一个回应的我的意见之一:
有一个圆形的参考,因此,它并没有结束。只有编译器
创建一个参考。这恰好是一个参考回
在其上创建的参考原始对象。这就是为什么你可以
做window.window.window ...喜欢这样:var OBJ = {}; obj.obj = OBJ ;.
只有一个创建的参考,但它是一个引用回
原来,所以你可以做obj.obj.obj.obj.obj.obj.obj === OBJ。
块引用>注释可以在这里看到:<一href=\"http://stackoverflow.com/questions/6679635/is-window-really-global-in-javascript/6680030#comment-8136449\">Is窗口在Javascript真正全球性的?
I can't understand why
window
is underself
andself
is underwindow
object.if you go to dev-tools or Firebug and write
window
you gotDOM window
object thatself
is under this object. The weird part is thatwindow
is underself
again!You can write
window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self.window.self
and still you get
window
object!How?!
解决方案In a response to one of my comments:
It is a circular reference, so it doesn't end. The compiler only creates one reference. It just happens to be a reference back to the original object on which the reference was created. That's why you can do window.window.window... Like this: var obj = {}; obj.obj = obj;. There's only one reference created but it's a reference back to the original, so you can do obj.obj.obj.obj.obj.obj.obj === obj.
Comment can be seen here: Is window really global in Javascript?
这篇关于在窗口对象奇怪的自我对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!