【发布时间】:2017-12-21 13:04:36
【问题描述】:
我想使用一个需要像这样创建对象并绑定到它的 javascript 库:
this.mystr = "hello";
this.webkitspeech = new webkitSpeechRecognition();
this.webkitspeech.onresult = function(evt) {
console.log(this.mystr); // this is undefined, even though I do have it defined
}
我通常会做一个.bind(this)
虽然在打字稿中我想这样做:
this.mystr = "hello"
this.webkitspeech = new webkitSpeechRecognition();
this.webkitspeech.onresult = onresult;
onresult(event) {
console.log(this.mystr) // this is undefined, even though I do have it defined
}
.bind(this) 在此示例中不起作用。我该如何解决这个问题?除了.bind(this),还有其他选择吗?或者任何适用于打字稿功能的东西?
【问题讨论】:
标签: javascript angular