I want to use a javascript library that requires creating an object and binding to it like this:
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
}
I would usually do a .bind(this)
Though in typescript I want to do 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
}
The .bind(this)
does not work in this example. How do I get around this? Are there alternatives to just doing .bind(this)
? Or whatever works for typescript functions?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…