I have this class in Javascript:
return function (settings) {
var items = [],
formHelper = new FormHelper(items),
that = this;
this.currentItem;
this.initialize = function () {
settings.listFn().then(function (response) {
angular.copy(response, items);
if (response.length > 0) {
that.setCurrentItem(response[0]);
}
});
}
}
In the initialize
method, I use a promise. When this promised is finished, the anonymous function is executed. Within the function you see the use of that
, which is declared at line 4. When I use the this
keyword instead of that
, this
points to window
, while that
points to the object.
I am wondering why this is. Could someone explain how this works? Why it behaves like this?