I would like to make a function that will create objects that have a tiered relationship with one another. So each tier object holds its own set of children tier objects and shares a single parent object with all of its siblings. I am unfamiliar with any patterns but I would guess there is one to cover this type of scenario.
//constructor
var Tier = function(parent){
if(parent===undefined)Tier.prototype.Parent = null;
else if(parent.constructor===Tier)Tier.prototype.Parent = parent;
else return //an error code;
//each tiered object should contain it's own set of children tiers
this.Children = [];
//...
//...additional properties...
//...
this.addChild = function(){
this.Children.Push(new Tier(this));
};
}
Tier.prototype.Parent; //I want this to be shared with all other tier objects on the same tier BUT this will share it between all tier objects regaurdless of what tier the object is on :(
Tier.prototype.Siblings; //should point to the parents child array to save on memory
Is it possible to create this kind of object where each tier object contains its own children and shares a parent object with its sibling but different tiers share the correct parents. I believe if I use something like the above when I add a new child it will make the Tier.prototype.Parent be the parent of that child but for all objects which isn’t the correct behaviour. I’m not sure how to get around this.
Any help much appreciated.
I don’t know if I’ve got it right, but try this:
I was a bit confused by your question so there could be something wrong with my answer.
As it is now, there can be a Tier with no parent. (no parameter) (
var newTier = new Tier(););If you create a new Tier with a parameter it will be added to the children in the parent node (
var parentTier = new Tier(); var child = new Tier(parentTier);).