Are there any performance issues or things to be wary of when things reference each other in such a way that they can be accessed infinitely?
Here's an example...
var Foo = function () {
var self = this;
this.setBar = function (bar) {
self.bar = bar;
self.bar.foo = self;
}
};
var Bar = function () {};
var foo = new Foo();
var bar = new Bar();
foo.setBar(bar);
// infinite references!
console.log(foo.bar.foo.bar.foo);
Some issues to be aware of:
JSON.stringify()
on an object that has circular references. In fact, the JSON format itself makes no allowances for specifying a circular reference.JSON.stringify()
in order to serialize data and/or send the flattened object over the wire.Set
or Map
object than it is in ES5.As long as you don't get caught in one of these circular iterations or have a need to represent your data structure in JSON, there's nothing particularly wrong with it - sometimes it is just the expedient way to do things.
The browser itself has many examples of such circular references. For example, the entire DOM has this as parent points to child and child points to parent.
node.firstChild.parentNode.firstChild.parentNode.firstChild.parentNode === node