我正在尝试将参数传递给回调函数中调用的函数,这可以使用 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/