Javascript 对象和 this

发布于 2024-12-11 06:39:14 字数 308 浏览 1 评论 0原文

function robot(robotId) {
  this.id = robotId;
  this.parts = new Array();

  this.collectParts = function() {
    $.getJSON('some/url', function(json) {
      for(i in json.responses) {
        this.parts = json.responses[i].parts;
      }
    });
  }
}

我如何实际分配 this.parts ?

function robot(robotId) {
  this.id = robotId;
  this.parts = new Array();

  this.collectParts = function() {
    $.getJSON('some/url', function(json) {
      for(i in json.responses) {
        this.parts = json.responses[i].parts;
      }
    });
  }
}

How do I actually assign this.parts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱殇璃 2024-12-18 06:39:14

将对 this 的引用(当它在适当的范围内时)分配给变量,并在更改了 this 范围的函数中使用该变量。在下面代码的修改版本中,robotInstance 是我选择使用的变量:

function robot(robotId) {
  var robotInstance = this;

  this.id = robotId;
  this.parts = new Array();

  this.collectParts = function() {
    $.getJSON('some/url', function(json) {
      for(i in json.responses) {
        robotInstance.parts = json.responses[i].parts;
      }
    });
  }
}

编辑: 我昨晚编写了此修改,然后决定不发布它。但根据@֪_._֪对你的问题的评论,我决定向你展示我编写代码的方式:

var robot = function( robotId )
{
  this.id = robotId;
  this.parts = [];
};
robot.prototype = {
  collectParts: function()
  {
    var robotInstance = this;

    $.getJSON(
      'some/url',
      function( json )
      {
        var i,
            responses = json.responses;
        for( i in responses )
        {
          if( Object.prototype.hasOwnProperty.call( responses, i ) )
          {
            robotInstance.parts = responses[i].parts;
          }
        }
      }
    );
  }
};

Assign a reference to this (when it's in the proper scope) to a variable and use that variable in the function which has changed the scope of this. In the modified version of your code below, robotInstance is the variable I've opted to use:

function robot(robotId) {
  var robotInstance = this;

  this.id = robotId;
  this.parts = new Array();

  this.collectParts = function() {
    $.getJSON('some/url', function(json) {
      for(i in json.responses) {
        robotInstance.parts = json.responses[i].parts;
      }
    });
  }
}

Edit: I had written this modification last night, then decided not to post it. But based on the comment to your question by @Ӫ_._Ӫ, I decided I'd show you the way I would write your code:

var robot = function( robotId )
{
  this.id = robotId;
  this.parts = [];
};
robot.prototype = {
  collectParts: function()
  {
    var robotInstance = this;

    $.getJSON(
      'some/url',
      function( json )
      {
        var i,
            responses = json.responses;
        for( i in responses )
        {
          if( Object.prototype.hasOwnProperty.call( responses, i ) )
          {
            robotInstance.parts = responses[i].parts;
          }
        }
      }
    );
  }
};
遗失的美好 2024-12-18 06:39:14

要在 jQuery 函数中访问 this,您需要将其分配给另一个变量,例如 self = this;,然后将 this 替换为 自我。

To access this inside jQuery functions, you need to assign it to another variable, for instance, self = this; then replace this with self.

南渊 2024-12-18 06:39:14

您可以在“that”变量中捕获“this”,以便可以在 $.getJSON 的回调范围内使用它

function robot(robotId) {
  this.id = robotId;
  this.parts = new Array();
  var that = this;
  this.collectParts = function() {
    $.getJSON('some/url', function(json) {
      for(i in json.responses) {
        that.parts = json.responses[i].parts;
      }
    });
  }
}

You can capture "this" in a "that" variable so that it can be used inside the scope of the callback of your $.getJSON

function robot(robotId) {
  this.id = robotId;
  this.parts = new Array();
  var that = this;
  this.collectParts = function() {
    $.getJSON('some/url', function(json) {
      for(i in json.responses) {
        that.parts = json.responses[i].parts;
      }
    });
  }
}
南冥有猫 2024-12-18 06:39:14

只需将其分配给另一个变量即可,例如

function robot(robotId) {
  var that = this; 
  that.id = robotId;
  that.parts = new Array();

  that.collectParts = function() {
    $.getJSON('some/url', function(json) {
      for(i in json.responses) {
        that.parts = json.responses[i].parts;
      }
    });
  }
}

Just assign this to another variable e.g that

function robot(robotId) {
  var that = this; 
  that.id = robotId;
  that.parts = new Array();

  that.collectParts = function() {
    $.getJSON('some/url', function(json) {
      for(i in json.responses) {
        that.parts = json.responses[i].parts;
      }
    });
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文