Javascript 在ajax中访问父对象的`this`变量
我知道Ajax是异步的,但我仍然无法理解对象作用域和javascript如何使用Javascript 在ajax中访问父对象的`this`变量,javascript,asynchronous,Javascript,Asynchronous,我知道Ajax是异步的,但我仍然无法理解对象作用域和javascript如何使用this工作 mylibrary = { my_variable: 0, my_function: function() { $.ajax({ url: "/theinternet", method: "POST",
this
工作
mylibrary = {
my_variable: 0,
my_function: function() {
$.ajax({
url: "/theinternet",
method: "POST",
data: {
// things
},
success: function(response) {
this.my_variable += 1; // How do I access my_variable?
console.log("HOORAY!");
},
error: function(error) {
console.log("ERROR");
}
// }).bind(this); this doesn't work.
}).bind(this);
}
}
我究竟应该如何访问ajax调用的success函数中的my_变量
这似乎是人们需要做的一件非常常见的事情,不是吗?使用对象名。更改
this.my_变量+=1代码>
到
mylibrary.my_变量+=1代码>使用对象名称。更改this.my_变量+=1代码>
到
mylibrary.my_变量+=1代码>解决方案
将my_function:function()
更改为arrow function()=>
语法应该可以解决问题
背景
问题是传递给ajax调用的this
的值来自my\u函数
,而不是my\u库
对象。这是因为函数
总是限定自己的此
,您可以使用箭头函数,该函数不会产生新的此解决方案
将my_function:function()
更改为arrow function()=>
语法应该可以解决问题
背景
问题是传递给ajax调用的this
的值来自my\u函数
,而不是my\u库
对象。这是因为函数
总是限定自己的此
,您可以使用箭头函数,该函数不会产生新的此您可以在ajax请求外部的变量中分配此
的引用,以便可以在ajax请求内部访问该变量。像这样的,
mylibrary = {
my_variable: 0,
my_function: function() {
var _this = this;
$.ajax({
url: "/theinternet",
method: "POST",
data: {
// things
},
success: function(response) {
_this.my_variable += 1; // How do I access my_variable?
console.log("HOORAY!");
},
error: function(error) {
console.log("ERROR");
}
});
}
}
在这段代码中,我们有var\u this=this
where\u此
可在$内访问。ajax
因为ajax请求位于my\u函数
中,\u此
位于引用此
的my\u函数的本地范围内。现在,当您使用\u this.my\u variable
时,它将更改对象mylibrary
的my\u variable
代码中的错误是没有将此
与函数绑定。您应该这样做以获得您的bind
工作
mylibrary = {
my_variable: 0,
my_function: function() {
$.ajax({
url: "/theinternet",
method: "POST",
data: {
// things
},
success: function(response) {
this.my_variable += 1; // How do I access my_variable?
console.log("HOORAY!");
},
error: function(error) {
console.log("ERROR");
}
});
}.bind(this);
}
您可以在ajax请求外部的变量中分配this
的引用,以便可以在ajax请求内部访问该变量。像这样的,
mylibrary = {
my_variable: 0,
my_function: function() {
var _this = this;
$.ajax({
url: "/theinternet",
method: "POST",
data: {
// things
},
success: function(response) {
_this.my_variable += 1; // How do I access my_variable?
console.log("HOORAY!");
},
error: function(error) {
console.log("ERROR");
}
});
}
}
在这段代码中,我们有var\u this=this
where\u此
可在$内访问。ajax
因为ajax请求位于my\u函数
中,\u此
位于引用此
的my\u函数的本地范围内。现在,当您使用\u this.my\u variable
时,它将更改对象mylibrary
的my\u variable
代码中的错误是没有将此
与函数绑定。您应该这样做以获得您的bind
工作
mylibrary = {
my_variable: 0,
my_function: function() {
$.ajax({
url: "/theinternet",
method: "POST",
data: {
// things
},
success: function(response) {
this.my_variable += 1; // How do I access my_variable?
console.log("HOORAY!");
},
error: function(error) {
console.log("ERROR");
}
});
}.bind(this);
}
您可以使用let _this=this
将父方法的范围分配给变量,并在内部函数中使用它
mylibrary = {
my_variable: 0,
my_function: function() {
let _this = this;
$.ajax({
url: "/theinternet",
method: "POST",
data: {
// things
},
success: function(response) {
_this.my_variable += 1; // How do I access my_variable?
console.log("HOORAY!");
},
error: function(error) {
console.log("ERROR");
}
// }).bind(this); this doesn't work.
}).bind(this);
}
}
您可以使用let _this=this
将父方法的范围分配给变量,并在内部函数中使用它
mylibrary = {
my_variable: 0,
my_function: function() {
let _this = this;
$.ajax({
url: "/theinternet",
method: "POST",
data: {
// things
},
success: function(response) {
_this.my_variable += 1; // How do I access my_variable?
console.log("HOORAY!");
},
error: function(error) {
console.log("ERROR");
}
// }).bind(this); this doesn't work.
}).bind(this);
}
}
您需要在函数表达式上使用bind
,而不是在$.ajax()
的返回值上。您需要在函数表达式上使用bind
,而不是在$.ajax()的返回值上使用bind
。