javascript のコンテキストとスコープについて理解できたと思いました。ある状況ではプロキシ/バインドが機能し、別の状況では機能しない理由がわかりません。誰か説明してください。
例 1: プロキシ (またはバインド) の動作:
function Cat(name){
this.name = name;
$("#cat").click(
$.proxy(
function(e){ this.meow(e); }
, this)
);
this.meow = function(){ alert(this.name + "says meow"); }
}
var cat = new Cat();
例 2: プロキシとバインドが機能しない:
function Dog(breed){
this.breed = breed;
this.save = function(){
var that = this;
$.ajax({
url: '/ajax/savedog.php', dataType: 'json',
// This works?? Shouldn't scope be of .ajax()?
data: this.breed,
// success: $.proxy(... // won't work? why?
success: that.dogSaved, error: ajaxFail
});
};
this.dogSaved = function(){ alert("Dog Saved"); }
}
var dog = new Dog();