You simply assign a link to an object. In the end, you only deal with links in ECMAscript. There is no such thing as an “actual object” with which you can deal in memory or clone it. Well, you can clone it, but it will just create another object in HEAP where you will get the link.
Think of it this way ...
var newObj = { };
Now it happens that a new object is formed / created somewhere on HEAP, and you have a link to this in your variable newObj. When we go like
newObj.foo = newObj
we are just referring to the same object somewhere on HEAP. No magic at all.
jAndy source
share