Reputation: 40778
How come I can store an object as it's own property? Here is a simple example:
let obj = {};
obj['obj'] = obj;
This will result in having an infinite object tree: I can call obj
with obj.obj.obj.obj
or even with obj.obj.obj.vobj.obj.obj.obj.obj
.
Is this an issue for performance? It doesn't seem to bother the browser at all.
Actually, when I look at the console in Chrome and click to expand obj
's properties it says (on the tooltip of i):
Value below was evaluated just now
So they were evaluated just when I clicked to expand.
Does this mean that JavaScript too will not look at obj
's property until I actually access them?
Is obj.obj
just a reference to obj
?
Upvotes: 2
Views: 407
Reputation: 138557
Is this an issue for performance?
No. A circular reference is just as every other reference. Every class instance has actually a circular reference:
instance.constructor.prototype.constructor.protototype
Is
obj.obj
just a reference toobj
?
Yes.
Does this mean that JavaScript too will not look at
obj
's property until I actually access them?
Yes. And the console won't try to expand it as it would get caught up in an endless loop.
Upvotes: 2