jQuery闭包解决ajax无法给外部变量赋值利用函数操作$.proxy()
function alter() {
var a = this;
this.three = 3;
this.four = 4;
this.callback = function () {
return function (data) {
data.parameter = a.four;
alert(a.three);
}
};
this.get = function () {
$.ajax({
type: "get",
url: "abcd.com",
beforeSend: function (XMLHttpRequest) {
//ShowLoading();
},
success: $.proxy(this.callback,this), //闭包返回函数,该函数自带上下文
error: function () {
//请求出错处理
}
});
}
}
var A = new alter();
A.get();