我对 javascript 模块和类感到困惑。我正在尝试创建我的类的对象,但它总是说“无法设置未定义的属性”或“对象没有方法”。有我的模块 2.js:
(function() {
var Game = function() {
this.state = 'a';
};
Game.prototype.somefunc = function() {
console.log(this.state);
};
})();
这是主要的应用程序代码:
var g = require('./2.js');
var mygame = new g.Game;
mygame.somefunc();
//undefined is not a function error
或者
var g = require('./2.js');
var mygame = g.Game;
mygame.somefunc();
//cannot call method of undefined error
我究竟做错了什么?