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?
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 ofthis. In the modified version of your code below,robotInstanceis the variable I’ve opted to use: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: