【发布时间】:2020-08-19 00:53:05
【问题描述】:
我在我的项目中使用jQuery Widget Factory,并且我正在尝试尽可能多地使用 ES6 语法,包括箭头函数。
那么,如何在小部件的方法中引用this?
作为一个假设的例子,我想转换这段代码
$.widget( "custom.colorize", {
// default options
options: {
red: 255,
green: 0,
blue: 0
},
// The constructor
_create: function() {
this.options.green = 128;
}
});
到这段代码(注意我把_create函数改成了箭头函数,会报错)
$.widget( "custom.colorize", {
// default options
options: {
red: 255,
green: 0,
blue: 0
},
// The constructor
_create: () => {
this.options.green = 128;
}
});
那么,我如何引用局部变量,因为 this 现在没有指向它们?
谢谢。
【问题讨论】:
标签: javascript jquery ecmascript-6 widget this