Chcem viazať funkciu na udalosť, ale chcem zmeniť kontext, v ktorom sa nazýva funkcia
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
}
}
odpovede:
5 pre odpoveď č. 1Môžeš použiť jQuery.proxy:
$("#selector").bind("click", $.proxy(this.foo,this) );
1 pre odpoveď č. 2
Skopírujte odkaz na objekt na lokálnu premennú a použite ju vo funkcii:
var me = this;
$("#selector").bind("click" function() { me.foo(); });
Ak potrebujete objekt udalosti, prejdite na:
var me = this;
$("#selector").bind("click" function(e) { me.foo(e); });