Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.window in JavaScript

Tags:

javascript

Why window object in the browser points to window object. Mozilla Website states the reason as

The point of having the window property refer to the object itself was (probably) to make it easy to refer to the global object (otherwise you'd have to do a manual var window = this; assignment at the top of your script).

So, my question is, how to infinitely point an object to object and how that helps to avoid doing var window = this;

window.window // returns window object
window.window.window // also returns an window
like image 426
GMR Avatar asked Mar 04 '16 05:03

GMR


People also ask

What is the window in JavaScript?

The window is the global object in the web browser. The window object exposes the functionality of the web browser. The window object provides methods for manipulating a window such as open() , resize() , resizeBy() , moveTo() , moveBy() , and close() .

How do you declare a window in JavaScript?

window. ABC = "abc"; //Visible outside the function var ABC = "abc"; // Not visible outside the function. If you are outside of a function declaring variables, they are equivalent.

What is window open in JavaScript?

Definition and Usage. The open() method opens a new browser window, or a new tab, depending on your browser settings and the parameter values.

What is window and document in JavaScript?

window is the execution context and global object for that context's JavaScript. document contains the DOM, initialized by parsing HTML. screen describes the physical display's full screen.


1 Answers

window.window or window.window.window and so on, that's not an implementation, that's a side-effect

consider this

var win = {};
win.win = win;

now

win.win === win

and

win.win.win === win

so what they could have done is like

var window = this;

which is actually same as

this.window = this;

because all the variables declared in global scope are properties of this, so doing such thing resulted in window.window.window.window....

like image 62
Ammar Hasan Avatar answered Oct 19 '22 12:10

Ammar Hasan