Javascript 在ajax中访问父对象的`this`变量

Javascript 在ajax中访问父对象的`this`变量,javascript,asynchronous,Javascript,Asynchronous,我知道Ajax是异步的,但我仍然无法理解对象作用域和javascript如何使用this工作 mylibrary = { my_variable: 0, my_function: function() { $.ajax({ url: "/theinternet", method: "POST",

我知道Ajax是异步的,但我仍然无法理解对象作用域和javascript如何使用
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