您的位置:首页 > Web前端 > JavaScript

js闭包解决ajax无法给外部变量赋值的问题

2017-03-21 00:00 615 查看
js闭包解决ajax无法给外部变量赋值的问题

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: this.callback(), //闭包返回函数,该函数自带上下文
error: function () {
//请求出错处理
}
});
}

}

var A = new alter();
A.get();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: