0

I was wondering despite of memory leak, what is the use of a circular reference in javascript? Even window object in the browser is circular referenced like window.window.window ...... Why we are using it and where we can use it. What are the good parts of it?

Praveen Poonia
  • 745
  • 10
  • 18
  • One use-case that comes to mind is for [polyfilling](https://github.com/polygonplanet/weakmap-polyfill/blob/master/weakmap-polyfill.js) the [`WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap) class. – Patrick Roberts Jan 21 '19 at 04:33
  • circular reference won't lead to memory leak in modern browsers – obfish Jan 21 '19 at 04:38
  • @obfish care to elaborate more? How do the garbage collection tell if the cyclic ref is still in use? – Samuel Toh Jan 21 '19 at 05:57
  • 2
    @SamuelToh https://stackoverflow.com/questions/7347203/circular-references-in-javascript-garbage-collector – Kaiido Jan 21 '19 at 06:08
  • @patrick well just `WeakMap = ...` would also work in non-strict mode. – Jonas Wilms Jan 21 '19 at 13:15
  • @JonasWilms I'm referring to the implementation, not the actual act of polyfilling... – Patrick Roberts Jan 21 '19 at 14:58

2 Answers2

2

I was wondering despite of memory leak,

There is no memory leak, two objects that reference each other but are not linked anywhere else get garbage collected. The window object will never be collected, so it doesn't matter here nevertheless.

what is the use of a circular reference in javascript?

Like in any other language they can be used for various structures, such as trees (parent <-> child), linked lists (left <-> right) and many-to-many relationships (students <-> classes). Not having them would complicate some forms of traversal and would make programs significantly slower.

Why is window.window a circular reference?

window is not only an object, but it is also the most global scope where all variables are finally looked up. When you use any global variable, such as setTimeout, it gets looked up in the global scope, and therefore the window object.

 window.setTimeout === /*window.*/setTimeout

Now if you want to refer to the global object, it has to be looked up in the global scope, which is itself the global object.

 window.window === /*window.*/window

Therefore just the window already accesses the circular reference, it is the reason why the global object can be found at all. Otherwise window would have to be a reserved keyword.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

see this page, great reasons are there Why window.window property exists?

The point of having the window property refer to the object itself, was likely 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.

Another reason, is that without this property you wouldn't be able to write, for example, "window.open('http://google.com/')". You'd have to use "open('http://google.com/')" instead.

.

and see this answer also https://stackoverflow.com/a/35788599/1475257

Abdelrahman Gobarah
  • 1,544
  • 2
  • 12
  • 29