function abc() {
var a = this;
this.p1 = 3;
this.p2 = 4;
this.callback = function () {
return function (data) {
var kk = a.p2;
alert(a.p1);
}
};
this.get = function () {
$.ajax({
type: "get",
url: "abcd.aspx",
beforeSend: function (XMLHttpRequest) {
//ShowLoading();
},
success: this.callback(), //闭包返回函数,该函数自带上下文
error: function () {
//请求出错处理
}
});
}
}
var A = new abc();
A.get();
jquerry里面有一个proxy,可以生成一个带上下文的方法
function abc() {
this.p1 = 3;
this.p2 = 4;
this.callback = function () {
alert(this.p1);
};
this.get = function () {
$.ajax({
type: "get",
url: "abcd.aspx",
beforeSend: function (XMLHttpRequest) {
//ShowLoading();
},
success: $.proxy(this.callback,this), //闭包返回函数,该函数自带上下文
error: function () {
//请求出错处理
}
});
}
}
var A = new abc();
A.get();