【发布时间】:2015-08-31 17:20:35
【问题描述】:
我在 ES6 class 中有一个函数:
class Test {
// Omitted code for brevity
loadEvents() {
$.get('/api/v1/events', (data) => {
this.actions.setEvents(data);
});
}
}
Babel 将其转换为不同的形式,并生成一个 _this 变量来控制箭头函数的词法范围。
var _this = this;
$.get('/api/v1/events', function (data) {
_this.actions.setEvents(data);
});
当我在 Chrome 中使用源映射调试 ES6 类并在我调用 this.actions.setEvents(data); 的行上放置一个断点时,我注意到 Chrome 没有“重新映射”this 以匹配原始 ES6 代码,而是 @ 987654327@ 指向外部函数作用域,如果我想访问箭头函数词法作用域,我需要使用_this,这完全没有意义。如果我使用源地图,我希望 Chrome 开发人员。在我的 ES6 代码中保留 this 的词法范围的工具。
有没有办法让 Chrome 开发者工具使用源映射和箭头函数按预期工作?
【问题讨论】:
-
"this 指向外部函数" - 我不清楚你的意思。
this的值是函数吗? “如果我想访问箭头函数范围,我需要使用 _this” -_this和this与函数的范围有什么关系?
标签: javascript google-chrome ecmascript-6 babeljs source-maps