【发布时间】:2020-01-17 08:18:45
【问题描述】:
这是我的 parent.class.js
class ParentClass {
constructor() {
}
}
module.exports = { ParentClass };
child.class.js
const { ParentClass } = require('./parent.class');
class ChildClass extends ParentClass {
constructor(index) {
super();
this.index = index;
}
showIndex() {
console.log(this.index)
}
}
module.exports = { ChildClass };
和 index.js 我正在使用子类的地方
const ChildClass = require('./child.class');
ChildClass(1).showIndex(); // This obviously is not working
注意:这只是一个例子。在实际项目中我无法使用 ES6 模块导入
我的问题是如何将参数传递给我的 index.js 中的子类实例?
如果是 ES6,我可以这样做:
import ChildClass from './child.class.js'
const ChildClass = new ChildClass(1)
ChildClass.showIndex(); // outputs 1 ...I guess??
如何将参数传递给子类?
【问题讨论】:
标签: javascript es6-class