javascriptextjs

Create link by reference to method in sub class


Is there any method to set link to sub class function by reference?

For example:

main program -> create object of classA which inside create object of classB
inside classB contain function-B

From main program if I want to call function-B I will have to use ClassA.ClassB.function-B()

If I want to call classA.function-B() , I will have to create function-B in classA which calls classB.function-B();

If I want to change function-B, I will have to change classB, then ClassA then main program.

I am looking for methods to create function-B in classA which by reference to classB function-B().

This way when I only need change function-B in classB and main program without need change classB.

Is there something like this? in classA function-B -> classB.function-B() ;


Solution

  • You can simplify it this way:

    class B {
    
      method() {
        console.log( this.constructor.name + '#method()' );
      }
    }
    
    class A {
    
      constructor() {
         this.b = new B;
      }
    
      method() {
        console.log( this.constructor.name + '#method()' );
          // invoke "b" method
        this.b.method();
      }
    }
    
    new A().method();