class TestArray extends Array<number>
{
    private _values;
    constructor(value) {
        super();
        this._values = value;
       
    }

    TestMethod() {
        alert("hello, world");
    }
}

var testArr = new TestArray([]);
testArr.TestMethod();

上面的代码可以编译通过,运行时提示TestMethod不存在,在构造函数中加上Object["setPrototypeOf"](this, TestArray.prototype); 就好了。

相关的链接

https://github.com/Microsoft/TypeScript/wiki/FAQ#why-doesnt-extending-built-ins-like-error-array-and-map-work

 

https://stackoverflow.com/questions/14000645/extend-native-javascript-array

 

标准的Error, Array, Map 都有这个问题。

 

class TestArray extends Array<number>
{
    private _values;
    constructor(value) {
        super();
        this._values = value;
        Object["setPrototypeOf"](this, TestArray.prototype);
    }

    TestMethod() {
        alert("hello, world");
    }
}

var testArr = new TestArray([]);
testArr.TestMethod();

 

相关文章:

  • 2022-01-27
  • 2022-12-23
  • 2021-09-24
  • 2021-07-02
  • 2021-12-05
  • 2021-09-07
  • 2022-12-23
  • 2022-01-06
猜你喜欢
  • 2021-05-30
  • 2021-12-26
  • 2021-11-04
  • 2022-12-23
  • 2022-12-23
  • 2021-06-06
  • 2021-11-05
相关资源
相似解决方案