我在javascript中声明了一个对象方法。 这个方法里面我想做一个ajax调用,当调用完成时,修改这个对象的一些属性。在另一个对象内部传递对象引用
Bubble.prototype.draw = function(){
console.log(this.attribute) // -> works fine
var req = $.ajax({
url: "someFile.php",
type: "post",
data: someData
});
// handle response
req.done(function (response, textStatus, jqXHR){
console.log(this.attribute) // -> not in the scope, obviously
});
}
我怎样才能把this.attribute
在req.done
范围有多大?如何访问req.done
内部的整个Bubble
对象?目前,所有我Bubble
S的是一个数组,所以我可以只通过在这个数组的索引和访问属性这样(array[i].attribute
)。我想有一个更好的方法来做到这一点。
你试过背景:这一点,因为阿贾克斯的选择吗?不知道它的工作原理在这种情况下 –
'this'需要保存在某个地方'$。阿贾克斯()'函数外部的变量。 'var self = this;'那么你可以使用'self'来访问'this'。 –