我正在为 Node.js 编写一个简单的服务器,我使用我自己的类 User,它看起来像:
function User(socket) {
this.socket = socket;
this.nickname = null;
/* ... just the typical source code like functions, variables and bugs ... */
this.write = function(object) {
this.socket.write(JSON.stringify(object));
}
};
然后在这个过程中,我将它实例化了很多次:
var server = net.createServer(function (socket) {
/* other bugs */
var user = new User(socket);
/* more bugs and bad practise */
});
我可以移动我的 User类定义到另一个 javascript 文件和“包含”它以某种方式?