javascript - ES6 将参数传递给从回调函数调用的函数

标签 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传递一个参数,该函数仅在鼠标移动时调用,这可能吗?

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

最佳答案

您还可以使用 es6 箭头函数而不是绑定(bind)来完成此操作。

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

关于javascript - ES6 将参数传递给从回调函数调用的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40319307/

相关文章:

javascript - 在捆绑的 JS reactjs 上需要未定义的错误

javascript - 运行随机数并保持状态

javascript - 循环 JS 对象仅适用于 return 语句,否则会抛出 undefined

javascript - 当方法分配给变量时,ES6 失去作用域

javascript - Pixi.js 轴对齐矩形 mask

javascript - 开放层 : How can I remove a feature?

javascript - 当调用设定的间隔函数时触发倒计时

javascript - 如何在 iFrame 内的父文档上使用 jQuery 命令?

d3.js - Pixi.js vs Konva.js vs D3.js

javascript - Chrome 使用 Pixi.js 加载 Sprite