Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在arrow函数中使用.emit()_Javascript_Ecmascript 6 - Fatal编程技术网

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)
相同。该绑定以后无法撤消或更改。欢迎使用
太糟糕了