我正在尝试通过子类化 Observable 并覆盖 lift 方法来继承 RxJS Observable 类,如此处所述。
这适用于我添加到原型中的任何运算符,但是每当我尝试实例化我的子类的新 Observable(例如,使用 MyObservable.from([1, 2, 3]))时,我都会得到父类的 Observable。
如何正确子类化 Observable,以便使用 Observable.return / Observable.from / 按预期工作?jsbin
class MyObservable extends Rx.Observable {
lift(operator) {
const observable = new MyObservable();
observable.source = this;
observable.operator = operator;
return observable;
}
customOperator() {
return this.map(arguments)
}
}
// instantiating an observable with **MyObservable**.from
var observable = MyObservable.from([1, 2, 3]);
console.log('instance of Rx.Observable: ' + observable instanceof Rx.Observable);
// this works as map is defined on RxObservable
observable
.map(value => console.log('standard ' + value))
.subscribe();
// this doesn't work. customOperator is only defined on MyObservable
// and MyObservable.from([1, 2, 3]) returns an Rx.Observable instance
observable
.customOperator(value => console.log('custom ' + value))
.subscribe();