未实现的类实现抽象类

时间:2016-04-21 23:08:03

标签: typescript

我不是Javascript方面的专家,而且我意识到我的跟随Typescript代码的问题从那里开始。但是我在最后36个小时就被困在这里,在Typescript中我有一个未定义的问题,我会让代码解释其余的:

window.onload = () => { var i = new IPropIndex(new IPropService()); };

interface IIDocService<T> {
    getSingle(): IPropDoc;
}

class IPropService implements IIDocService<T> {
    getSingle(): IPropDoc { return new IPropDoc(); }
}

abstract class IDocIndex<T> {
    constructor(public _IDocType: string, public _IIDocService: IIDocService<T>) {
        console.log(this._IDocType); //NOT UNDEFINED
        this.methodB();
    }

    public abstract methodA();

    public methodB() {
        this.methodA(this.methodC); //**UNDEFINED**
    }

    public methodC() {
        console.log(this._IDocType);
    }
}

class IPropIndex extends IDocIndex<IPropDoc> {
    constructor(public _IPropService: IPropService) {
        super('iPROP', _IPropService);
        console.log(this._IPropService); //NOT UNDEFINED
    }

    public methodA(callback: any): void {
        console.log(this._IPropService); //**UNDEFINED**
        callback();
    }
}

class IPropDoc { public name: string; }

如果没有在代码中签名未定义的问题,我怎样才能拥有此解决方案!? 感谢。

1 个答案:

答案 0 :(得分:1)

  

的console.log(this._IPropService); // <强> UNDEFINED

你明显错了this

修复

使用箭头

public methodA = (callback: any) => {
    console.log(this._IPropService); //**UNDEFINED**
    callback();
}

更多:https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html