gpt4 book ai didi

javascript - JS : access from NEW inner object, 父容器创建者对象方法/变量

转载 作者:行者123 更新时间:2023-12-03 00:39:51 25 4
gpt4 key购买 nike

大家下午好。

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

让我们假设一个如下示例:

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 需要被告知它的容器是什么。容器可以作为参数传递给构造函数。

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

关于javascript - JS : access from NEW inner object, 父容器创建者对象方法/变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53507803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com