简体   繁体   中英

Javascript Access to parent properties from child method

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM