Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6720265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:11:31+00:00 2026-05-26T09:11:31+00:00

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

  • 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;
      }
    });
  }
}

How do I actually assign this.parts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:11:31+00:00Added an answer on May 26, 2026 at 9:11 am

    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;
              }
            }
          }
        );
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I typically use the below function to return the root URL if I ever
I use to do this on AS2 code. this.pause3Seconds = function () { trace(_global.something(_root.somthing));
function returnsAnArray () { return array ('test'); } echo returnsAnArray ()[0]; generates a syntax
function register_contact ($person = array()) { $nogood = false; foreach ($person as $val) {
I have a function that counts registry items and added a new option to
I can't get this work with MySQL 5.x. This function should return all relevent
I have the following code. The angle function needs some information from the class
This is perhaps quite a simple one for some of you. I was looking
t.grow = function(parent,evt) { evt.target.Y = 0 var r = parent.radius, that = this
Consider the following jasmine spec: describe(something.act(), function() { it(calls some function of my module,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.