関数をイベントにバインドしたいが、関数が呼び出されるコンテキストを変更したい
function Foo(){
t : "123",
bar: function(){
// I am here
$("#selector").bind("click" this.foo);
}
,
foo: function(){
//I want when to be able to use the current object here by this
//this.t should be 123 and this.bar should call the above function
}
}
回答:
回答№1は5あなたは使うことができます jQuery.proxy:
$("#selector").bind("click", $.proxy(this.foo,this) );
回答№2の場合は1
オブジェクトへの参照をローカル変数にコピーし、関数で使用します。
var me = this;
$("#selector").bind("click" function() { me.foo(); });
イベントオブジェクトが必要な場合は、それを渡します:
var me = this;
$("#selector").bind("click" function(e) { me.foo(e); });