2016-11-15 4 views
0

の「この」変化値Iは、次のコードをお持ちの場合:私はfunctionTwoのthis.functionOne()内部を置き換えるためにSomething.prototype.functionOne()を使用する必要はありませんきれいな方法でこれを書くことができますどのようにクリック

function Something() { 
    this.randomElement = $("#element"); 
} 

Something.prototype = { 

    functionOne: function() { 
     console.log("hello"); 
    }, 

    functionTwo: function() { 
     this.randomElement.click(function(e) { 
      this.functionOne(); //this becomes pointed at randomElement 
     }); 
    } 
} 

を? clickイベントはこの値を変更するので、

答えて

3

thisはクリックされたアイテムにバインドされているためです。あなたはbind

this.randomElement.click(this.functionOne.bind(this)); 

やjQueryのproxyを使用する必要があります。

this.randomElement.click($.proxy(this.functionOne, this)); 
関連する問題