I do not have enough reputation to comment :-( so I will answer the question of Usman Iqbal as an answer. Maybe some administrators can convert this into a comment.
Implicit callback functions create a structure called closure: an object containing the real function, as well as the context of its creation. This context contains all the variables surrounding the callback, as well as their values ββat the time the function was created. A closure does not contain this context because it is an object itself and has its own this at the time the callback is executed.
To make this from the creation context directly accessible, the arrow functions, by definition, process this , which is not closing this , but this creating context. Without arrow functions, you can achieve the same thing by explicitly copying the surrounding this into the closure context:
let self = this; // this of the creation context geocoder.geocode(request, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { if (results[0] != null) { ... // access the surrounding this via self self.shareService.setLocationDetails(city); } ... } });
trsft source share