JavaScriptOOP-jQuery调用;这";内部ajax请求
我想发出一个ajax请求,它仍然保留对当前对象的访问权。有人知道这是否可能吗 我想做的示例:JavaScriptOOP-jQuery调用;这";内部ajax请求,javascript,jquery,oop,scope,Javascript,Jquery,Oop,Scope,我想发出一个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 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);
}
});
}
}
}
function mobile\u as(作为对象,inputID){
var self=this;//你知道self是一个引用还是一个副本吗?我之前做过,但我担心如果我这样做,并且实际对象在请求过程中被修改,我将本质上创建一个竞争条件。或者使用它会创建一个引用吗?@Tom对象在JavaScript中总是通过引用分配的。@jrummell这是主观的,不是吗?我不这么认为,但你可以随意不同意。那些认为OOP意味着“类”的人通常对任何编程语言中的OOP都很反感。