Incluindo a definição de classe JavaScript de outro arquivo no Node.js
Estou escrevendo um servidor simples para o Node.js e estou usando minha própria classe chamadaUser
que se parece com:
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));
}
};
e, mais tarde, no processo, estou instanciando muito:
var server = net.createServer(function (socket) {
/* other bugs */
var user = new User(socket);
/* more bugs and bad practise */
});
Posso mover meuUser
definição de classe para outro arquivo javascript e "incluí-lo" de alguma forma?