Reputation: 912
In the code I have written, an object has as one of its properties, another object. Within this second object, a property exists that points back to the parent object. Now when I view the object in firebug, I can endlessly dive through this self-referential loop.
Is this poor programming? What are the repercussions of such a setup? Is there a better way to set up access to a parent object through its child?
This is a simplified example of what I have:
function Entity(){
this.subEntity = {parent:null};
}
function createEntity(){
var entity = new Entity();
entity.subEntity.parent = entity;
}
createEntity();
The purpose for needing the parent of an object is because I am using a library which creates specific objects that I have wrapped into my own outer-objects. The library's methods point to its own objects, but sometimes I would like to point back to my outer-object to access its methods. Thanks.
As per Kevin B, here is a more specific peek into my situation:
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.
Hope that helps. Perhaps I should write a method that returns the parent? Not sure how to reference it in the method though, unless I made the parent a property of the fixture, which is back to my old problem.
Upvotes: 0
Views: 111
Reputation: 324680
window.window.window.window.window.window.window.window.......
Backreferences can actually be useful. For example, a <canvas>
's rendering context object has a .canvas
property pointing back to the canvas. While it's true you could just keep track of the canvas yourself, it just makes it easier to reference things.
Upvotes: 2