从另一个 JS 文件(不是游戏状态)Phaser js 调用函数

Calling Function from another JS file(not a game state) Phaser js

import Timer from '../components/Timer.js';

export default class MainGame extends Phaser.State {
    preload() {
        console.log("create");
    }
    create() {
        this.timerJS = new Timer(this.game);
    }
    update() {

    }
}

以上代码是我的MainGame状态代码。在创建中,我创建了 Timer.js 对象。现在,如何调用写在 Timer.js 文件中的 foo()。 Timer.js不是状态,只是一个简单的js文件。

如果实例化 class 的新变量引用,您可以简单地从您创建的变量中调用 class 上的方法。

所以在这种情况下,要调用方法 foo(),只需:

this.timerJS.foo()