admin管理员组文章数量:1426092
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?
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?
Share Improve this question edited Oct 21, 2011 at 2:45 mkly asked Oct 21, 2011 at 2:41 mklymkly 2,2732 gold badges18 silver badges23 bronze badges 2-
1
Too many of the same response, so I'll leave a ment. There are a couple other ways you could fix this that don't involve a variable. One is the
jQuery.proxy()
method, which will return a function that will invoke your function but with the correctthis
bound. Another solution would be to use thejQuery.ajax()
method instead, and set thecontext:
parameter, which allows you to set the value ofthis
in the callbacks. – user113716 Commented Oct 21, 2011 at 2:53 -
1
If you do use a variable, I'd place it inside the
collectParts
function. Doing this, or using the above approaches will let you place yourcollectParts
method onrobot.prototype
, so you're not recreating the same function with every instance. – user113716 Commented Oct 21, 2011 at 2:59
4 Answers
Reset to default 6Assign 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 ment 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;
}
}
}
);
}
};
To access this
inside jQuery functions, you need to assign it to another variable, for instance, self = this;
then replace this
with self
.
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;
}
});
}
}
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;
}
});
}
}
本文标签: jqueryJavascript object and thisStack Overflow
版权声明:本文标题:jquery - Javascript object and this - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745467489a2659594.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论