ser*_*0ne 1 javascript arrays inheritance prototype typescript
我有一些继承自Array<T>......的实验代码
class SuperArray<T> extends Array<T> {
constructor(...items) {
super(...items);
}
public clear(): void {
while (this.length > 0) {
this.pop();
}
}
}
var array = new SuperArray("Matt", "Mark", "Luke", "John");
array.clear();
Run Code Online (Sandbox Code Playgroud)
我添加了这个clear方法只是为了说明问题.当它在浏览器中编译并运行时,我得到......
TypeError:array.clear不是函数
这是TypeScript中完全有效的代码,但在JavaScript中无效,有没有办法解决这个问题?
顺便说一下,这是TS2.1的一个突破性变化
此TS文档建议在构造函数中更改原型
constructor(...items) {
super(...items);
Object.setPrototypeOf(this, SuperArray.prototype);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
897 次 |
| 最近记录: |