我不是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; }
如果没有在代码中签名未定义的问题,我怎样才能拥有此解决方案!? 感谢。
答案 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