[英]Javascript OOP - jQuery calling “this” inside ajax request
我想提出一個仍然保留對當前對象的訪問權限的ajax請求。 有人知道這是否可能嗎?
我想做的事的例子:
function mobile_as(as_object, inputID) {
this.object = 'something';
if (as_object) this.object = as_object;
this.inputID = inputID;
// Get results for later usage.
this.get_results = function(value) {
this.request = $.getJSON("URL", { as: this.object }, function (data) {
// Scope of "this" is lost since this function is triggered later on.
if (data['status'] == "OK") {
alert(this.inputID);
}
});
}
}
}
營救結束時間:
function mobile_as(as_object, inputID) {
var self = this; // <---------
this.object = 'something';
if (as_object) this.object = as_object;
this.inputID = inputID;
// Get results for later usage.
this.get_results = function(value) {
this.request = $.getJSON("URL", { as: this.object }, function (data) {
// Scope of "this" is lost since this function is triggered later on.
self.... //self is the old this
if (data['status'] == "OK") {
alert(self.inputID);
}
});
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.