Matrym
Matrym

Reputation: 17073

Subclassing in Javascript / OOJS

I've got a class "A" that has multiple instances. I've got a class "B" that has multiple instances per instance of class "A". The class "B" doesn't need special methods depending on the instance of its parent, but it does need to be able to trigger methods that effect specifically its parent instance. How do I structure this relationship?

To be clear, I'm not describing "Animal --> Human" (a subset). I'm describing "Human --> Tools", whereby the parent has many instances of something, and those instances should have access to methods that affect only their possessive parent instance.

Upvotes: 0

Views: 766

Answers (1)

DuckMaestro
DuckMaestro

Reputation: 15915

var TypeA = function() { };
TypeA.prototype.doSomething = function() {/*...*/};

var TypeB = function(parent) { this._parent = parent; }
TypeB.prototype.doSomethingToParent = function() { this._parent.doSomething(); }

When you instantiate B, always pass in your current A.

If I understand your question correctly?

Upvotes: 2

Related Questions