我想这是很正常的问题,但由于某种原因,我在网上找不到答案。
我有一个A班:
class A {
const data = {...}
}
和一个单独的js文件中的B类。
class B {
// how can I get const data here?
}
发布于 2018-11-22 01:30:35
您可以使用组合,并在类A
的构造函数中实例化类B
的新实例。
class A {
constructor() {
this.data = { foo: "bar" };
}
}
class B {
constructor() {
this.instanceOfA = new A();
console.log(this.instanceOfA.data);
}
}
console.log(new B());
发布于 2018-11-22 03:29:51
下面是两个类中按函数/事件传递的数据:
class A {
constructor(name) {
console.log(name)
}
}
class B {
y(){
return "ram"
}
}
let resultFromB = (new B().y());
new A(resultFromB)
https://stackoverflow.com/questions/53427374
复制相似问题