这个问题可能听起来有点困惑,所以我让代码解释一下:
function Foo(arg) {
const argument = arg;
const fooPart = new FooPart(this);
this.printArg = function() {
console.log(argument);
}
}
function FooPart(foo) {
this.parent = foo;
this.parent.printArg();
}
let foo = new Foo("this is the argument");
这对我不起作用。我怎样才能解决这个问题或更好地解决这个问题 - 正确的方法是什么?
谢谢
最佳答案
function Foo(arg) {
this.argument = arg;
this.fooPart = new FooPart(this);
}
Foo.prototype.printArg = function() {
console.log(this.argument);
}
function FooPart(foo) {
this.parent = foo;
this.parent.printArg();
}
let foo = new Foo("this is the argument");
- 你应该在
printArg
定义之后调用FooPart
- 您应该使用
this.parent
访问parent
关于javascript - 如何在构造函数中创建一个 JS 对象并将 "parent object"作为构造函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57496183/