mardi 22 décembre 2020

Javascript return property instead of object during object initialization

is there way to change, what object actual is, when we manipulate with it? For exaple, if I have this class:

class Parent{
    child;
    contructor(){
        child = new Child();
    }
}
class Child{
    sayHello(){
        console.log("Hello");
    }
}

When I create instance of Parent class, I want to manipulate directly with its child property (which is actually reference to another object). I want to be able to do this:

let myObj = new Parent();
myObj.sayHello()

Sorry for silly/poor explanation, I really don't know right terminology for this.

Is there way to do what I want? Or any solution which give "similar" result to what I want? Notice, that what I really don't want is to solve it something like this:

let myObj = new Parent();
myObj.child.sayHello()

Thanks!

Aucun commentaire:

Enregistrer un commentaire