TypeError: t.printEleis not a fu

2020-01-20  本文已影响0人  前端老邹_伯通
class MyArray extends Array {
    constructor() {
        super();
    }

    public printEle(): void {
        for (let i = 0; i < this.length; i++) {
            console.log(this[i]);
        }
    }
}

let t = new MyArray();
t.push(1, 2, 3);
t.printEle();
TypeError: t.printEleis not a function
class MyArray extends Array {
    constructor() {
        super();
        // 注册 MyArray类的原型
        Object.setPrototypeOf(this, MyArray.prototype)
    }

    public printEle(): void {
        for (let i = 0; i < this.length; i++) {
            console.log(this[i]);
        }
    }
}

let t = new MyArray();
t.push(1, 2, 3);
t.printEle();

参考:https://blog.simontest.net/extend-array-with-typescript-965cc1134b3

上一篇下一篇

猜你喜欢

热点阅读