Browserify - 提及同一文件中的函数

Browserify - Mentioning a function from the same file

我正在使用 browserify 做我的 JavaScript,但我很难记住如何做。

var account = require('./account');

module.exports = {
        start: function() {
            console.log('Logging');    
            module.music(this, 'game-bg.mp3');
        },
        music: function(arg, soundfile) {
            console.log('Playing music...');
            if (arg.mp3) {
                if(arg.mp3.paused) arg.mp3.play();
                else arg.mp3.pause();
            } else {
                arg.mp3 = new Audio(soundfile);
                arg.mp3.play();
            }
        }
};

当我 运行 时,我得到 Uncaught TypeError: module.music is not a function 并且它永远不会开始播放音乐。

我必须做什么才能使其正常工作?我尝试查找主题,但找不到提到多个函数的主题。

我认为如果你想在一个对象中引用另一个函数(并且你没有创建 class 所以没有 this),那么只分配你的东西会更清楚也更容易想要导出到一个变量,然后再导出该变量。像这样:

var account = require('./account');

var player = {
        start: function() {
            console.log('Logging');    
            player.music(this, 'game-bg.mp3');
        },
        music: function(arg, soundfile) {
            console.log('Playing music...');
            if (arg.mp3) {
                if(arg.mp3.paused) arg.mp3.play();
                else arg.mp3.pause();
            } else {
                arg.mp3 = new Audio(soundfile);
                arg.mp3.play();
            }
        }
};

module.exports = player;