ES6避免/自我 [英] ES6 avoid that/self

查看:120
本文介绍了ES6避免/自我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图避免const that = this,const self = this等使用es6。
然而,我正在努力结合vue js和highcharts的一些结构,你有这样的事情:

  data (){
let that = this
return {
highchartsConfiguration:{
... big configuration ...
formatter:function(){
返回this.point.y + that.unit
}
}
}
}

如果可能的话,我想在formatter对象中定义它。使用箭头语法()=> {}我可以在数据作用域中使用它,但我会失去赋予函数额外作用域的权力。



如果你需要这个 >调用 formatter 方法时,没有办法绕过一个额外的变量( self ,无论如何)。


I am trying to avoid "const that = this", "const self = this" etc. using es6. However I am struggling with some constructs in combination of vue js and highcharts where you got something like this:

data () {
  let that = this
  return {
    highchartsConfiguration: {
      ... big configuration ...
      formatter: function () {
        return this.point.y + that.unit
      }
    }
  }
}

I'd like to have the that defined just in formatter object if possible. Using arrow syntax () => {} would me allow to use this from data scope, but i would lose the power of giving the function an extra scope.

I do not want to modify the used libraries.

解决方案

If you need the this that the formatter method is called on, there is no way around an extra variable (that, self, whatever).

这篇关于ES6避免/自我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆