Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am studying a book on Javascript, "Javascript: The Definitive Guide - David Flanagan". Chapter 3 of this book talks about the Global object, here, they say that

global Window object has a self-referential window property that can be used instead of this to refer to the global object.

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

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.

like image 808
Roy Avatar asked Jul 05 '26 16:07

Roy


1 Answers

Self-referential means that the Window object has a property which references itself.

window.window = window

When you're in the window scope, this === window so you can reference properties like window.location using the following methods.

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!