2
私は、オブジェクトを作成し、このようにそれに結合する必要がJavaScriptライブラリを使用したい:angular2タイスクリプトで「バインド」を正しく行うにはどうすればいいですか?
:私はこれをしたい、私は通常、typescriptですで.bind(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
}
を
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)
をやっているだけの選択肢がありますか?あるいは、typescript関数には何が使えますか?このような
this.webkitspeech.onresult = ($event) => { this.onresult($event) };
または使用bind
:
this.webkitspeech.onresult = this.onresult.bind(this);
それとも機能をバインドするための最も便利な方法活字体ならびにES6で
https://github.com/Microsoft/TypeScript/wiki/%27this%27-in-TypeScript#fixes – yurzui
@yurzuiもお読みください。これは私が言及した3つの方法です。私は答えに参照を入れました –
this.mystrへの参照を取得する方法は?定義されているか定義されていないWiIl? – Rolando