Search code examples
javascriptoopprototypal-inheritancecircular-reference

JS: access from NEW inner object, parent container creator object methods/variables


Good afternoon everyone.

I need to know if something like this is even possible and how:

Let's assume an example like below:

function ObjectB(pFoo){
  //Some object vars/properties
  this.varX = '';
  
  //Some object methods
  this.methodX = function(){
    //...
    //HERE. is where I want to call the function/method from "my container/parent", which is an instanced ObjectA. How can I call for example, "method2()" from ObjectA?
    //...
  };
  this.methodY = function(){
    //...
    console.log(this.varX);
    //...
  };
  
  //Constructor time
  this.varX = pFoo;
}

function ObjectA(pA, pB){
  //Some object vars/properties
  this.var1 = '';
  this.var2 = '';
  this.innerObjB = null;
  
  //Some object methods
  this.method1 = function(){
    //...
    this.innerObjB.methodY(); //No problem at all: calls method from it's own inner "var/property" self object.
    //...
  };
  this.method2 = function(){
    //...
    this.var2 = 'trololo';
    //...
  };
  this.method3 = function(){
    //...
    this.innerObjB.methodX();
    //...
  };
  this.method4 = function(){
    //...
    console.log(this.var2);
    //...
  };
  
  //Constructor time
  this.var1 = pA;
  this.var2 = pB;
  this.innerObjB = new ObjectB("whatever");
}

//Runtime
var ObjA = new ObjectA("blah", "bleh");
ObjA.method1(); //prints "whatever".
ObjA.method4(); //prints "bleh".
ObjA.method3(); //calls innerObjB.methodX(), which SHOULD call ObjectA method2().
ObjA.method4(); //If previous thing were resolved, now this should print "trololo".

How can I achieve this? How do I make ObjectB methodX(), to call its "container/parent" (not a real parent, as this is not inheritance) ObjectA already instantiated method2()?

What I've thought, is to pass as a parameter to Object B, the "this", from object A, like:

this.innerObjB = new ObjectB("whatever", this);

This way, I will have "full objectA" inside ObjectB to access. Already instantiated and fully functional. But this creates a deep-hole in my mid: isn't that a rare kind of "recursivity" dependency? As you can acces from B, A again, and from that A acces B and again, never end the loop. So this doesn't make much sense at all...

Thanks for your time.

Kind regards,

Mark.


Solution

  • ObjectB needs to be told what its container is. The container can be passed as a parameter to the constructor.

    function ObjectB(pFoo, container) {
      //Some object vars/properties
      this.varX = '';
      this.container = container;
    
      //Some object methods
      this.methodX = function() {
        this.container.method2();
      };
      this.methodY = function() {
        //...
        console.log(this.varX);
        //...
      };
    
      //Constructor time
      this.varX = pFoo;
    }
    
    function ObjectA(pA, pB) {
      //Some object vars/properties
      this.var1 = '';
      this.var2 = '';
      this.innerObjB = null;
    
      //Some object methods
      this.method1 = function() {
        //...
        this.innerObjB.methodY(); //No problem at all: calls method from it's own inner "var/property" self object.
        //...
      };
      this.method2 = function() {
        //...
        this.var2 = 'trololo';
        //...
      };
      this.method3 = function() {
        //...
        this.innerObjB.methodX();
        //...
      };
      this.method4 = function() {
        //...
        console.log(this.var2);
        //...
      };
    
      //Constructor time
      this.var1 = pA;
      this.var2 = pB;
      this.innerObjB = new ObjectB("whatever", this);
    }
    
    //Runtime
    var ObjA = new ObjectA("blah", "bleh");
    ObjA.method1(); //prints "whatever".
    ObjA.method4(); //prints "bleh".
    ObjA.method3(); //calls innerObjB.methodX(), which SHOULD call ObjectA method2().
    ObjA.method4(); //If previous thing were resolved, now this should print "trololo".