简体   繁体   English

客户端javascript中的自引用窗口属性是什么?

[英]What is self-referential window property in client-side javascript?

I am studying a book on Javascript, " Javascript: The Definitive Guide - David Flanagan ". 我正在学习一本关于Javascript的书,“ Javascript:The Definitive Guide - David Flanagan ”。 Chapter 3 of this book talks about the Global object, here, they say that 本书的第3章讨论了Global对象,在这里,他们说

global Window object has a self-referential window property that can be used instead of this to refer to the global object. 全局Window对象具有自引用窗口属性 ,可以使用 属性代替 属性来引用全局对象。

What I understand from the above line is that window is not the object instead it is self-reference, but could someone explain me in detail how it is.. and how to create a self-referential property for a custom object. 我从上面这一行中理解的是,窗口不是对象,而是自引用,但有人可以详细解释它是如何...以及如何为自定义对象创建自引用属性。

Like in chrome console if I type in window i get 就像在Chrome控制台中一样,如果我输入窗口,我会得到

Window {top: Window, location: Location, document: document, window: Window, external: Object…} 窗口{顶部:窗口,位置:位置,文档:文档,窗口:窗口,外部:对象...}

How to achieve the same for custom objects. 如何为自定义对象实现相同的功能。 Sorry, if I understood this totally wrong please excuse me for that, I am newbie to JS. 对不起,如果我理解这完全错了,请原谅我,我是JS的新手。

Self-referential means that the Window object has a property which references itself. 自引用意味着Window对象具有引用自身的属性。

window.window = window

When you're in the window scope, this === window so you can reference properties like window.location using the following methods. 当您在window范围内时, this === window以便您可以使用以下方法引用window.location属性。

  1. window.location
  2. this.location
  3. location
  4. window.window.location
  5. this.window.location
  6. etc... 等等...

You understood it wrong. 你明白错了。 It means that the window object has a member called window which is a reference to the window object itself. 这意味着window对象有一个名为window的成员,它是对window对象本身的引用。 That is to say, 也就是说,

window.window === window

Adding some quotes to the quote might clarify it a bit: 在报价中添加一些引号可能会稍微澄清一下:

global Window object has a self-referential "window" property... 全局Window对象具有自引用“窗口”属性...

(ie global Window object has a property called "window" that is self-referential.) (即全局Window对象具有一个称为“window”的属性,它是自引用的。)

Although it's very rarely useful, to create one for a custom object you just assign itself to a member element. 虽然它很少有用,但是为自定义对象创建一个,你只需将自己分配给一个成员元素。

var obj = {};
obj.obj = obj;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM