【问题标题】:JS: access from NEW inner object, parent container creator object methods/variablesJS:从新的内部对象访问,父容器创建者对象方法/变量
【发布时间】:2018-11-27 20:41:54
【问题描述】:

大家下午好。

我需要知道这样的事情是否可行以及如何:

让我们假设如下示例:

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

我怎样才能做到这一点?如何使 ObjectB 的 methodX() 调用它的“容器/父级”(不是真正的父级,因为这不是继承)ObjectA 已经实例化了 method2()?

我的想法是作为参数从对象 A 传递给对象 B,即“this”,例如:

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

这样,我将在 ObjectB 中访问“完整的 objectA”。已经实例化并且功能齐全。 但这在我的中间造成了一个深洞:这不是一种罕见的“递归”依赖吗?因为您可以再次从 B 访问 A,然后从该 A 访问 B 再一次,永远不要结束循环。所以这根本没有多大意义......

感谢您的宝贵时间。

亲切的问候,

马克。

【问题讨论】:

  • 不清楚您要在这里创建什么对象模型。如果您正在合成,那么通常您不会 这样做,如果是,ObjectB 实例将需要ObjectA 实例来调用其函数。 ObjectBObjectA是什么关系?
  • 听起来像javascript对象继承
  • 好吧不是继承。想象一下,objectA 是“House()”。在这里面,你有一个“Persons()”对象和一个“Furnitures()”对象。然后你有一些方法基本上建立了它们之间的相互关系,在 House() 对象级别,例如,方法“userSaveToCloset(u, c)”。但是,在某些时候,在 Persons() 对象内部,您可能需要调用该精确方法。并且在 Persons() 类中重新定义它不是一种选择。像这样的东西。所以你看,不是继承,而是各自概念之间的关系。
  • 两个对象之间的循环引用没有任何问题。 (不,这里不涉及递归,也不会尝试无休止地遍历属性。

标签: javascript oop prototypal-inheritance circular-reference


【解决方案1】:

ObjectB 需要被告知它的容器是什么。容器可以作为参数传递给构造函数。

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

【讨论】:

  • 这正是我所评论的,但它有一个问题:当您在构建时将容器上下文传递给内部对象时,它会收到一个“上下文”,其中他自己尚未完全创建,所以例如您尝试从子对象内部访问父容器,然后再从这里访问在这种情况下引用的子容器,您不能并且它失败了,因为它还没有被创建......至少那是我所拥有的测试。我知道这很肮脏,但在那种情况下,让所有东西都被创建并最终通过特定方法分配“this”可能会更好。不在构造函数上。
  • 只有ObjectB 构造函数尝试调用容器上的方法时才会出现问题。如果构造函数只存储对容器的引用,如我所示,没有问题。在容器完全构建之后,您才开始调用方法。
猜你喜欢
  • 2016-11-12
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 2016-12-27
  • 2013-04-28
  • 1970-01-01
  • 2014-05-30
  • 1970-01-01
相关资源
最近更新 更多