2017-02-20 9 views
0

consoelエラーが発生し続ける:予期しないトークンthisどうすればいいですか?

この機能を動作させるにはどうすればよいですか? 、あなたはnewキーワードを使用する場合

var _this = this; 
this.Map = new Button({ 
    text: "Map", 
    press: function(event){ 
     pop.open(_this._Map); 
    } 
}); 

答えて

0

あなたはこのように、あなたのプレス機能にこれをバインドする必要がありインタプリタは新しい空オブジェクトを作成し、コンストラクタ内の "this"というキーワードをバインドしたコンストラクタ(ここではButton)を にバインドして呼び出しますこれと同じではありませんthis.map

あなたは明示的にButton()を呼び出していないので、これをバインドすることはできません。あなたは次のように行うことができます :

var self = this; 
this.Map = new Button({ 
      text: "Map", 
      press: function(event){ 
       pop.open(self._Map); 
      } 
     }) 
0

function onPress(event){ 
    pop.open(this._Map); 
} 

this.Map = new Button({ 
    text: "Map", 
    press: onPress.bind(this) 
}) 

またはあなたはまた、このアプローチを使用することができます。

 this.Map = new Button({ //button is a control so dont worry if it doesn't make sense 
      text: "Map", 
      press: function(event){ 
       pop.open(this._Map); 
      } 
     }) 
0

あなたはまた、ES6の構文を使用することができます。

press: (event) => { 
    pop.open(this._Map); 
} 

はここにおよそarrow functions

を読みます
関連する問題