Possible Duplicate:
Why doesn't this closure have access to the 'this' keyword? – jQuery
function class1() {
this.url = 'http://en.wikiquote.org/w/api.php?action=query&format=json&prop=revisions&titles=Albert_Einstein&rvprop=content&callback=?';
this.f = function() {
$.getJSON(this.url, function() {
console.log(this.url);
});
}
};
var obj = new class1();
obj.f();
Instead of printing the url, obj.f() prints “undefined” to the console. Why is this happening and what can I do to prevent this behaviour?
Inside your ajax callback,
thiswill be the jqXhr object, not your current object. You’ll want to save the value ofthisbefore your ajax call, and reference that in your callback: