Javascript 在arrow函数中使用.emit()
在维护Javascript 在arrow函数中使用.emit(),javascript,ecmascript-6,Javascript,Ecmascript 6,在维护this.emit()的同时,尝试找出如何执行箭头函数。在gulp和ES6中,我有这样一个函数: gulp.src([paths.sass]) .pipe(sourcemaps.init()) .pipe(sass().on('error', function (e) { reportError(e); this.emit('end'); })) 注意这个.emit('end')的用法。当我使用非箭头功能时效果很好,但第二次使用时: gul
this.emit()
的同时,尝试找出如何执行箭头函数。在gulp和ES6中,我有这样一个函数:
gulp.src([paths.sass])
.pipe(sourcemaps.init())
.pipe(sass().on('error', function (e) {
reportError(e);
this.emit('end');
}))
注意这个.emit('end')的用法。当我使用非箭头功能时效果很好,但第二次使用时:
gulp.src([paths.sass])
.pipe(sourcemaps.init())
.pipe(sass().on('error', (e) => {
reportError(e);
this.emit('end');
}))
此.emit('end')
不再可用。如何使用arrow函数编写该函数并维护this.emit()
在ECMAScript 2015的arrow函数中,此
始终引用函数外部,没有运行时上下文
测量:
function out() {
var name = "out"
return function() {
return function() {
return function() {
console.log(this.name) // out
}
}
}
}
- 箭头函数无对象
参数
- 箭头函数不能使用
产生
- 箭头功能无法通过
new
sass()
返回对象的on
方法安排调用提供的函数作为另一个对象的方法,并且该函数需要提取调用它的对象的值,为了调用它的emit
方法,请使用函数
声明
箭头函数避免使用
Function.prototype.bind
定义上下文的当前this
值,方法是始终在函数中使用定义上下文的this
值。箭头函数的另一个方面是它们不能用作构造函数。箭头函数不反对函数声明和表达式,只应视具体情况视为“首选” 不能,箭头函数没有this
。箭头函数绑定到它们周围的上下文–与function(e){}.bind(this)
相同。该绑定以后无法撤消或更改。欢迎使用此
太糟糕了