You are developing an HTML5 web application that displays customer mailing address information. The application loads addresses from a web service by using AJAX.
The following code defines a Customer object and loads address data.
You need to implement the loadAddress function. Which code segment should you use?
A.
$.get(‘data.xml’, function (data) {
(function (scope) {
scope.parseAddress(data);
})(data);
});
B.
$.get(‘data.xml’, function (data, innerScope) {
innerScope.parseAddress(data);
});
C.
var that = this;
$.get(‘data.xml’, function (data) {
that.parseAddress(data);
}
D.
$.get(‘data.xml’, function (data) {
this.parseAddress(data);
}
C
what’s the difference between C and D?
Why do have I to set the value to a var of “this” operator instead of using it directly?
I’m wondering this too…
I think maybe because we want ‘this’ to refer to loadAddress function and not to the function we are writing (function(data))…
Can anybody tell me if that’s correct?
Yes that is correct var that = this; will reference the customer.prototype.parseAddress= function(data)
D is wrong because it gets the current instance or context of the function inside the get request, what we want is the instance or context of the load address method this is why we assign the this keyword to a variable named that. from inside the load address method we perform the get request to the parse address method.