【发布时间】:2017-05-27 20:45:30
【问题描述】:
我想从Constructor2 引用Constructor1 (property1) 中的一个属性
我想,这样做没问题...还是应该将constructor2 嵌套在constructor1 中?
var Constructor2 = function() {
this.method2 = function() {
// how to reference Constructor1.property ???
};
};
var Constructor1 = function() {
this.property1 = true;
this.property2 = false;
this.method1 = new Constructor2();
};
var inst = new Constructor1();
inst.method1.method2();
【问题讨论】:
-
GForce和Constructor2一样吗? -
您需要将
inst作为参数传递给method2,或者从method1的构造函数中创建对它的引用。 -
该方法根本不在构造函数上。它在它返回的实例上。
this不是指构造函数,而是调用new Constructor1()时将返回的对象。 -
是的,我弄错了 GForce is Constructor2
标签: javascript object constructor