1
如果B扩展A,那么A是否可以定义一个创建新B的方法?如何在打字稿中的基类中创建相同类型的实例
class SetA {
constructor(public items:any[]) {
}
createNew(items){
return new *typeof this*(items); //<-- insert actually working magic here
}
clone(){
return this.createNew(this.items);
}
}
class SetB extends SetA { }
var x = new SetB([1,2,3]);
x.clone(); //<-- returns a new SetB
完美,谢谢 – Flion