坏习惯?对象中指向父对象的对象-Javascript

坏习惯?对象中指向父对象的对象-Javascript,javascript,Javascript,在我编写的代码中,一个对象的一个属性是另一个对象。在第二个对象中,存在一个指向父对象的属性。现在,当我在firebug中查看对象时,我可以无休止地浏览这个自引用循环 这是糟糕的编程吗?这样的设置有什么影响?有没有更好的方法设置通过其子对象访问父对象的权限 这是我所拥有的一个简化示例: function Entity(){ this.subEntity = {parent:null}; } function createEntity(){ var entity = new Entity(

在我编写的代码中,一个对象的一个属性是另一个对象。在第二个对象中,存在一个指向父对象的属性。现在,当我在firebug中查看对象时,我可以无休止地浏览这个自引用循环

这是糟糕的编程吗?这样的设置有什么影响?有没有更好的方法设置通过其子对象访问父对象的权限

这是我所拥有的一个简化示例:

function Entity(){
  this.subEntity = {parent:null};
}

function createEntity(){
  var entity = new Entity();
  entity.subEntity.parent = entity;
}
createEntity();
需要对象父对象的目的是因为我正在使用一个库来创建特定的对象,我已经将这些对象包装到自己的外部对象中。库的方法指向它自己的对象,但有时我想返回到我的外部对象来访问它的方法。谢谢

根据Kevin B的说法,以下是对我的情况的更具体的了解:

var b2Listener = Box2D.Dynamics.b2ContactListener //library object
var listener = new b2Listener;

//Next I overwrite a method in order to gain control of it, 
//which receives two arguments, one being a contact object which contains the two
//fixtures which collided, and the impulse is the force. 

listener.PostSolve = function(contact, impulse){ 
  conA = contact.GetFixtureA()
}
//But I have the fixtures wrapped in my own objects which have properties and methods
//outside the fixture which I would like to access within the PostSolve method.

希望有帮助。也许我应该写一个返回父级的方法?但不确定如何在方法中引用它,除非我将父对象作为夹具的属性,这又回到了我以前的问题。

window.window.window.window.window.window.window……


反向引用实际上是有用的。例如,
的呈现上下文对象具有指向画布的
.canvas
属性。确实,你可以自己跟踪画布,这只是让参考更容易。

你能展示一个你需要
父对象
的案例吗?@KevinB就这样。除了必须序列化数据的情况外,我看不出这有什么问题。序列化循环不在许多序列化标准中。好的,很好。我只是不确定我的浏览器是否在那里处理来来回回的引用。它有限制吗,或者它只是在有东西剥开一层之前不处理它们?