javascript - Accessing properties from inside callback ES2015 -
this question has answer here:
i'm using classes in es2015 identical snippet below:
class profilemanager{ constructor($http){ this._http = $http; this.profile = {}; } getuser(profile){ this._http(`/api/users/${user.id}`).then(function(response){ this.profile = response.data; /* * results in exception because "this uses function's * "this" instead of class' "this" */ }); }
i know can remedy creating profile variable outside of class , using in class, wondering if there cleaner or more preferred way use class properties inside of nested function or callback.
es6 arrow functions not override this
class profilemanager { constructor($http) { this._http = $http; this.profile = {}; } getuser(profile) { this._http(`/api/users/${user.id}`).then((response) => { this.profile = response.data; }); }
Comments
Post a Comment