WITH TypeScrip:无法从函数内部引用&;#39;This&;#39;(类) [英] With TypeScript: unable to refer to 'this' (class) from inside a function
本文介绍了WITH TypeScrip:无法从函数内部引用&;#39;This&;#39;(类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习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关键字确定为驱动程序。
这篇关于WITH TypeScrip:无法从函数内部引用&;#39;This&;#39;(类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文