0

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.

4
  • It's unclear what object model you're trying to create here. If you're compositing then in general you wouldn't do that, and if you were, ObjectB instances would need an ObjectA instance to call its function on. What is the relationship of ObjectB to ObjectA? Commented Nov 27, 2018 at 20:58
  • Sounds like javascript object inheritance Commented Nov 27, 2018 at 21:11
  • Well is not inheritance. Just imagine, objectA is "House()". and inside this, you have an "Persons()" object and a "Furnitures()" object. And then you have some methods that basically establish interrelations between those, on House() object level, for example, method "userSaveToCloset(u, c)". But then, could happen that in some point, inside Persons() object, you need to call that precise method. And to redefinite this inside Persons() class is not an option. Something like this. So you see, is not inheritance, but relationshiping between their own conceptions. Commented Nov 27, 2018 at 22:09
  • 2
    Nothing is wrong with the circular reference between the two objects. (No, there's no recursion involved here, and nothing will try to endlessly traverse the properties. Commented Nov 27, 2018 at 22:28

1 Answer 1

1

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".

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

2 Comments

That's precisely what I commented, but it has a problem: as you pass container context to inner object on construction time, it receives a "context" where himself is not already fully created, so if for example you try to acces from inside children object, to parent container and from this, again to the children referenced in this case, you can't and it fails, because it was not created yet... At least that is what I've tested. I know it's sordid, but in that case, maybe is even better to let everything being created and in the end, assign "this" through a specific method. Not on constructor.
That's only a problem if the ObjectB constructor tries to call methods on the container. If the constructor just stores the reference to the container, as I show, there's no problem. You don't start calling methods until later, after the container is fully constructed.

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.