jQuery - get reference to this
function Request(params)
{
// Stuff stuff stuff
// And then
$.ajax(
{
type: 'GET',
url: 'so开发者_开发百科meurl',
success: this.done
});
}
Request.prototype.done = function()
{
// "this" in this context will not refer to the Request instance.
// How to reach it?
}
You could capture "this" first:
function Request(params)
{
// Stuff stuff stuff
// And then
var $this = this;
$.ajax(
{
type: 'GET',
url: 'someurl',
success: function() { $this.done(); }
});
}
Apparently you can add the "context" parameter to the ajax request, like so:
$.ajax(
{
type: 'GET',
url: 'someurl',
success: this.done,
context: this
});
this
is not reffering to the same thing!!!
try following:
function Request(params)
{
var that = this;
....
Request.prototype.done = function()
{
that...
精彩评论