JS:从 NEW 内部对象访问,父容器创建者对象 methods/variables
JS: access from NEW inner object, parent container creator object methods/variables
大家下午好
我想知道这样的事情是否可能以及如何实现:
让我们假设如下示例:
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(),调用它的 "container/parent"(不是真正的父级,因为这不是继承)ObjectA 已经实例化了 method2()?
我的想法是将对象 A 的 "this" 作为参数传递给对象 B,例如:
this.innerObjB = new ObjectB("whatever", this);
这样,我就可以在 ObjectB 中访问 "full objectA"。已经实例化并且功能齐全。
但这在我心中造成了一个深洞:这不是一种罕见的 "recursivity" 依赖吗?正如您可以再次从 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".
大家下午好
我想知道这样的事情是否可能以及如何实现:
让我们假设如下示例:
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(),调用它的 "container/parent"(不是真正的父级,因为这不是继承)ObjectA 已经实例化了 method2()?
我的想法是将对象 A 的 "this" 作为参数传递给对象 B,例如:
this.innerObjB = new ObjectB("whatever", this);
这样,我就可以在 ObjectB 中访问 "full objectA"。已经实例化并且功能齐全。 但这在我心中造成了一个深洞:这不是一种罕见的 "recursivity" 依赖吗?正如您可以再次从 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".