0

I have a Campaign object with a campaign_offer array property which contains N CampaignOffer children.

My goal is to access to the Campaign object inside each CampaignOffer object.

To achieve this, I've added in the CampaignOffer object a parent property which contains the Campaign object.

// Parent
var Campaign = function (properties) {
    this.id = properties.id || null;
    this.campaign_offer = [];
};

// Parent: Add a new child in array of children
Campaign.prototype.addOffer = function (offer) {
    // Pass the parent reference to the child
    offer.parent = this;

    this.campaign_offer.push(offer);
};

// Child
var CampaignOffer = function (properties) {
    this.name = properties.name || null;
    this.parent = properties.parent || null;
};

// ---------------------------------------------

// Create the parent
var campaign = new Campaign({ id: 1 });

// Create the child
var campaign_offer = new CampaignOffer({ name: 'test' });

console.log('Offer without parent', campaign_offer);

// Add the child to the parent
campaign.addOffer(campaign_offer);

console.log('Offer with parent', campaign_offer);

You can see the result there: Fiddle

The problem is that when you browse the second console.log(). You can find too much recursiveness. Like:

parent.campaign_offer[0].parent.campaign_offer[0].parent.campaign_offer[0]...

I understand this output but don't know how to avoid this. What about defining a max depths?

Btw it doesn't make an infinite loop.

2
  • 5
    Your objects have a reference to each other - every time you expand the properties, you see the same references. It's not a problem - you just have to understand that it will happen while you're inspecting stuff. Commented Oct 15, 2015 at 13:01
  • 3
    Why does the relationshp have to be two-way? I can see why CampaignOffer might need to reference a Campaign object, but I can't see any reason for the converse. (I'd also call it campaign rather than parent, but that's a style thing.) You can do that, and it's harmless unless you write code that tries to traverse it and doesn't understand that it may circle back, but I don't see any need for it. Commented Oct 15, 2015 at 13:01

1 Answer 1

2

I understand this output but don't know how to avoid this. What about defining a max depths?

You don't have multiple depths here, to speak of, you just have a circular reference:

  /---------------------------------------------------------------------\
  |  +----------------+                                                 |
  +->|    campaign    |                                                 |
     +----------------+                                                 |
     | id: xxx        |         +----------+                            |
     | campaign_offer |-------->| (array)  |                            |
     +----------------+         |----------+        +----------------+  |
                                | 0        |------->| campaign_offer |  |
                                +----------+        +----------------+  |
                                                    | name: xxx      |  |
                                                    | parent         |--/
                                                    +----------------+

Circular references are fine provided you don't write code traversing them that is unaware that they may exist, as that code may end up looping forever.

I don't see any need for one in this case, it seems like CampaignOffer having a Campaign makes sense, but I don't see much point to the converse (the campaign_offer array). But again, it's fine, just don't write code that tries to follow these to their end, as they don't have an end.

Sign up to request clarification or add additional context in comments.

2 Comments

@JamesThorpe: :-) Love me some ASCII-art.
Thanks dude & all replyers! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.