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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -