Javascript 如何在jquery回调es6中访问该类

Javascript 如何在jquery回调es6中访问该类,javascript,jquery,ecmascript-6,es6-class,Javascript,Jquery,Ecmascript 6,Es6 Class,上面是es6类中的一个方法。原始脚本如下所示: events ( node ) { let thisclass = this; node.on('mouseover', ( d, i ) => { thisclass.animateParentChain( d ); }); node.on('mouseout', (d, i) => { d3.select(t

上面是es6类中的一个方法。原始脚本如下所示:

    events ( node ) {

        let thisclass = this;

        node.on('mouseover', ( d, i ) => {
            thisclass.animateParentChain( d );
        });

        node.on('mouseout', (d, i) => {
            d3.select(this).select("circle").attr("r", 2.5);
            thisclass.unAnimateParentChain( d );
        });

        node.on('click', (nd, i) => {
            thisclass.persistsAnimateParentChain( nd );
            thisclass.loadModal(nd);
        });
    }
新的es6方法可以使用function关键字重写,例如:

events: function ( node ) {
        node.on('mouseover', function ( d, i ) {
            app.radialD3.animateParentChain( d );
        });
        node.on('mouseout', function(d, i){
            d3.select(this).select("circle").attr("r", 2.5);
            app.radialD3.unAnimateParentChain( d );
        });
        node.on('click', function(nd, i){
            app.radialD3.persistsAnimateParentChain( nd );
            app.radialD3.loadModal(nd);
        });
    },
更改为使用函数关键字,一切正常。如果没有,脚本将出现
关键字的问题

我对es6有些陌生,但写这篇文章的“正确”方式是什么呢。在回调中,我需要
this
作为html元素而不是类,但同时回调需要访问类的方法。使用function关键字似乎是在倒退,将当前类设置为变量也是如此:/


如何区分
this
的呢?

你不能让
this
同时引用两个不同的东西,所以是的,你应该使用
函数
并像你一样通过闭包访问类实例。这样做的“正确”方法是不需要使用
this
来访问html元素。难道你不能通过
节点
d
e.currentTarget
或类似的方式访问它吗?“使用function关键字似乎是在倒退”函数表达式/声明和箭头函数是两种不同的工具。箭头函数不是现有函数定义的(完全)替代品,因此它绝对不是倒退。你必须为这项工作使用正确的工具。
events ( node ) {

        let thisclass = this;

        node.on('mouseover', function ( d, i ) {
            thisclass.animateParentChain( d );
        });

        node.on('mouseout', function (d, i) {
            d3.select(this).select("circle").attr("r", 2.5);
            thisclass.unAnimateParentChain( d );
        });

        node.on('click', function(nd, i){
            thisclass.persistsAnimateParentChain( nd );
            thisclass.loadModal(nd);
        });
    }