如何使用更改默认值的库来调用它?

how to call this with libraries that changing the default this?

所以我开始使用 interactjs 我有这个简单的代码:

class example {
    registerTouchEvents() {
        var self = this;
        interact('.touchy').draggable({
            onstart: self.onStart,
        });
    }

    onStart(event) {
        this.someAction();//<-- not working as this is interact object
    }

    someAction() {
        console.log('touch has been started') //<-- I need to call this function
    }

}

有没有不用全局变量调用当前对象的方法?

将处理程序移动到您声明的位置 "self":

class example {
    registerTouchEvents() {
        var self = this
          , onStart = function onStart(event) {
                self .someAction();
            }
          ;
        interact('.touchy').draggable({
            onstart: onStart,
        });
    }

    someAction() {
        console.log('touch has been started') //<-- I need to call this function
    }

}