Google is not being helpful trying to find the answer to this question! 🙁
How do I properly reference this.colour from the parent object from within a jQuery function like this:
var obj = {
colour: 'blue',
do: function() {
$.getJSON('getcolour.php', function(resp) {
if (resp.colour == this.colour) { //<== this.colour doesnt = blue
//match
}
});
}
}
You need to obtain a reference to
thisoutside of the call to$.getJSON:alternatively, on ES5 browsers, use
.bindto set the context of the inner callback tothis:jQuery’s
$.proxy()function can also be used on earlier browsers to achieve the same effect.