ES6 Pass paremeter功能从回调功能调用

时间:2016-10-29 12:56:17

标签: javascript ecmascript-6 pixi.js

我正在尝试将参数传递给在回调函数中调用的函数,是否可以使用es6或javascript?这是以下示例:

   sprite.on('mousedown', this.onDragStart)
         .on('touchstart', this.onDragStart)
         .on('mouseup', this.onDragEnd)
         .on('mouseupoutside', this.onDragEnd)
         .on('touchend', this.onDragEnd)
         .on('touchendoutside', this.onDragEnd)
         .on('mousemove', this.onDragMove);
         .on('touchmove', this.onDragMove);



   onDragStart(event) {
           this.data = event.data;
           this.alpha = 0.5;
           this.dragging = true;
           this.selected  = true;
       }

    onDragMove(arg) {

        if (this.dragging) {
            var newPosition = this.data.getLocalPosition(this.parent);
            arg.m_orange.setPosition(newPosition.x, newPosition.y);
        }
    }

    onDragEnd() {
        this.alpha = 1;
        this.dragging = false;
        this.data = null;
        this.selected = false;
    }

我想将一个参数传递给函数onDragMove,只有在鼠标移动时才会调用它,这是可能的吗?

如果不是,还有其他方法可以将父类的关键字传递给onDragMove函数吗?

2 个答案:

答案 0 :(得分:1)

您也可以使用es6 arrow-func而不是bind。

on('mousemove', (ev) => this.onDragMove(ev, otherArg))

答案 1 :(得分:0)

您可以使用Function.prototype.bind

on('mousemove', this.onDragMove.bind(thisArg, otherArg)) 
相关问题