我正在创建一个名为Carousel的类,并希望从该类中调用一个名为Pips的类,该类将通过父子关系绑定到carousel。
我遇到的问题是我收到以下错误:
Super expression must either be null or a function, not undefined
我当前的代码如下所示:
// carousel.js
import { Pips } from './pips'
export class Carousel {
constructor( options ) {
this.pip_type = options.pip_type;
}
setup() {
this.initPips();
}
initPips() {
var p = new Pips(
{
carousel_name : 'carousel',
pip_type : this.pip_type
}
);
}
}
// pips.js
import { Carousel } from './carousel'
export class Pips extends Carousel {
constructor(options) {
super(options);
console.log( 'Pips Initialised' );
}
}很明显,存在循环依赖,但是当我试图用一个位于单独文件中的类扩展另一个类时,我看不到如何用ES6语法来避免这种情况-如果我从pips.js中删除import { Carousel }...,那么我只会得到Carousel is undefined的错误。
如果我将Pips类移动到与我的Carousel类相同的文件中,然后在它下面,那么我不会得到这个错误,所以我认为问题可能与ES6提升导入有关。
对于我来说,在这两个类之间建立这种关系的最佳方式是什么?
发布于 2016-12-17 18:14:20
这更多的是类层次结构中的设计问题。超类不应该知道和/或使用对子类的引用。
也许你应该使用组合,而不是尝试做循环引用。
我真的不明白为什么Pips继承了Carousel。
希望这能有所帮助。
https://stackoverflow.com/questions/41197369
复制相似问题