0

I have two classes, parent and child and I want the child class to access a method on the parent class in some way (note that child class doesn't extend/inherit from parent class)... The parent class just has a property of the array of it's children objects.

class Parent {
  constructor() {
    this.name = "parent";
    this.children = [];
  }

  addChild(child) {
    this.children.push(child);
  }

  foo() {
    console.log("foo");
  }
}

class Child {
  constructor(name) {
    this.name = name;

    // Call the foo function of the parent
  }

}

I also discovered an alternative approach:

class Child {
  constructor(name, parent) {
    this.name = name;
    this.parent = parent;
    this.parent.foo();
  }
}

But in the above approach, each child has its parent class as a property on it but the parent class also has the child property so, it would create a situation where the child contains the parent which contains the child which again contains the parent and so on..... And I don't think that it is the best programming practice to have infinitely nested properties on an object, So I'm a bit hesitant to this approach.

I hope there is some other way to doing this, or is the infinitely nested property approach fine?

9
  • What do you mean by "the parent"? You can add the same child to multiple parents. Commented Dec 27, 2021 at 16:16
  • There's no automatic link to the object that contains a reference to another object. If you need that link, you have to make it explicitly, as in your second approach. Commented Dec 27, 2021 at 16:18
  • I believe in order to have a parent child relationship they must have been defined in a nested fashion such as the Child class should have been defined under the Parent class. Commented Dec 27, 2021 at 16:19
  • @Barmar So, is the second approach ok to use in terms of programming practices, because it gets the job done but I don't know if it will scale well as more child classes are added.... Commented Dec 27, 2021 at 16:20
  • 1
    Yes, it's how you're supposed to do it. Commented Dec 27, 2021 at 16:20

1 Answer 1

1

The parent-child relationship has to be established and maintained explicitly for each child. And since the parent property of each child instance/object is just a reference to a Parent instance, the OP does not need to worry about memory consumption or whatever it is, the OP refers to as "nested".

class Child {
  constructor(name, parent) {

    this.name = name;
    this.parent = parent;

    // Call the `foo` function of the parent
    // in case `parent.foo` exists and is callable.
    parent?.foo?.();
  }
}

class Parent {
  constructor() {
    this.name = 'parent';
    this.children = [];
  }

  addChild(referenceOrName) {
    if (typeof referenceOrName === 'string') {

      this.children.push(new Child(referenceOrName, this));
    } else if (
      (referenceOrName instanceof Child) &&
      !this.children.includes(referenceOrName)
      //!this.children.find(child => child.name === referenceOrName.name)
    ) {
      referenceOrName.parent = this;
      this.children.push(referenceOrName);
    }
  }

  foo() {
    console.log("foo");
  }
}

const testParent = new Parent;
const testChild = new Child('baz', testParent);

console.log({ testParent });

testParent.addChild('bar');
testParent.addChild(testChild);

console.log({ testParent });

testParent.addChild('biz');
testParent.addChild('boz');

console.log({ testParent });
.as-console-wrapper { min-height: 100%!important; top: 0; }

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

2 Comments

So, to sum it up, it is fine to have infinitely nested objects as long as the deeply nested properties are not called, right?
Nope. Twice. There is nothing deeply nested within a simple parent-child relationship and the child carrying a reference to its parent. Also references are not "expensive". And of cause accessing something constantly / heavily like foo.bar.baz.biz.buz is of cause more expensive than doing it via a reference like const biz = foo.bar.baz.biz and then constantly accessing biz.buz.

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.