从回调ES2015内部访问属性
Accessing properties from inside callback ES2015
我在ES2015中使用的类与下面的片段相同:
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 the function's
* "this" instead of the class' "this"
*/
});
}
我知道我可以通过在类外创建一个概要文件变量并在类中使用它来解决这个问题,但我想知道是否有更干净或更首选的方法可以在嵌套函数或回调中使用类属性。
ES6箭头函数不覆盖this
class ProfileManager {
constructor($http) {
this._http = $http;
this.profile = {};
}
getUser(profile) {
this._http(`/api/users/${user.id}`).then((response) => {
this.profile = response.data;
});
}
相关文章:
- HTML5拖放访问属性
- 如何从保存在数组某个位置的JSON行访问属性
- webGL,javascript:尝试访问属性1中超出范围的顶点
- 访问属性的权限被拒绝'filepicker_comm_iframe'
- CanvasJS-访问变量-访问属性toString的权限被拒绝
- 错误:访问属性“”的权限被拒绝;文件”;
- 从javascript访问属性多端数组PHP
- 模块中的访问属性(CommonJS 样式)
- 从控制台 js 访问属性
- 相同的域,但仍收到“访问属性'文档'的权限被拒绝”
- 如何在 Javascript 中从函数本身或其副本访问属性
- 属性 Javascript 的访问属性
- 从 CoffeeScript 中类的属性中的方法访问属性的属性
- 通过 attrs.val 或 attrs.$set(attname, val)访问属性
- 使用 WinJS.Class 和访问属性
- Twitch TV API 访问属性名称值并更新它
- 如何访问属性名称中具有特殊字符的javascript对象的属性
- Cordova-不推荐尝试访问属性'userAgent'在非导航器对象上
- 具有私有方法的JS原型类不访问属性
- iFrame中的SelectionContext-访问属性“getSelection”的权限被拒绝