我在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'。 –