如何使用函数作为类参数

时间:2019-05-11 14:01:59

标签: javascript pixi.js

我是JS的新手,我花了一些时间学习AS3,但是我却错过了JS背后的许多逻辑。我试图将一个函数作为类的构造函数参数传递,当从构造函数本身调用该函数时,它可以工作,但是一旦我尝试从类内部的函数中调用它,它就表示该函数不是函数

我尝试了很多地方在类中声明该函数,到目前为止没有任何效果,我尝试使用2个参数(类/属性),就像在as3中一样,但是也没有成功。

class ButtonSimple {
    constructor(value) {
        this.callFunction = value;
        this.textureButton = PIXI.Texture.fromImage("image link");
        this.button = new PIXI.Sprite(this.textureButton);
        this.button.buttonMode = true;
        this.button.anchor.set(0.5);
        this.button.x = 50;
        this.button.y = 50;
        this.button.interactive = true;
        this.button.buttonMode = true;
        this.button
        .on('pointerdown', this.onButtonDown);
        this.callFunction() // this one works
    }
    onButtonDown() {
        this.isdown = true;
        this.callFunction(); // this one doesn't
    }
    GetSprite() {
        return this.button;
    }
}

var testThis = function () {
    console.log("working");
}

var myButton = new ButtonSimple(testThis);
app.stage.addChild(myButton.button);

好吧,这是日志:

app.js:24 Uncaught TypeError: this.callBack is not a function
at e.onButtonDown (app.js:24)
at e.a.emit (index.js:181)
at e.dispatchEvent (InteractionManager.js:906)
at e.processPointerDown (InteractionManager.js:1186)
at e.processInteractive (InteractionManager.js:1061)
at e.processInteractive (InteractionManager.js:1027)
at e.onPointerDown (InteractionManager.js:1149)

0 个答案:

没有答案