What is the client-side self-referential window property in javascript?

I am studying a Javascript book, " Javascript: The Definitive Guide - David Flanagan . Chapter 3 of this book talks about a global object, it says that

The global Window object has a self-referential window property, which can instead of this to refer to the global object.

What I understand from the above line is that the window is not an object, but a self-reference, but can someone explain to me in detail how this happens ... and how to create a self-referential property for a custom object.

Like in the chrome console, if I type a window , I get

Window {top: Window, location: Location, document: document, window: Window, external: Object ...}

How to achieve the same for custom objects. Sorry if I understood this completely wrong, sorry for that, I'm new to JS.

+4
source share
2 answers

Self-referential means that the Window object has a property that refers to itself.

 window.window = window 

When you are in the Window scope, this === window so that you can reference properties such as window.location using the following methods.

  • window.location
  • this.location
  • location
  • window.window.location
  • this.window.location
  • etc...
+3
source

You got it wrong. This means that the window object has a member named window , which is a reference to the window object itself. I.e

 window.window === window 

Adding some quotes to the quote may explain this a bit:

the global Window object has the self-referential "window" property ...

(i.e., the global Window object has a window property that is self-reflective.)

Although it is very rarely useful to create it for a custom object, you simply assign a member element.

 var obj = {}; obj.obj = obj; 
+2
source

All Articles