我正在学习TypeScript
,有以下课程:
class DetailDriver {
public get driver() {
return super.getEntity();
}
public activate(): breeze.Promise {
var id = this.driver.id(); // this refers to (class) DetailDriver
return promise
.then(getCertificate)
.fail(somethingWrong);
function getCertificate() {
var id = this.driver.id(); // this refers to any
return ...
}
}
}
正如您在上面的代码中看到的,对this
的第一个调用引用了我的类DetailDriver
。那很好。对this
的第二个调用(在getCertificate
内)引用any
。这不是我需要的。我需要参考一下我的课程DetailDriver
。
如何继续?
谢谢。
好,
根据TypeScrip语言规范的第4.9.2节,您应该使用胖箭头语法来保留其作用域。
return promise
.then(() => return.this.id;)
.fail(somethingWrong);
则正确地将This关键字确定为驱动程序。