在Node.js中创建模块的副本而不是实例 [英] Create a copy of a module instead of an instance in Node.js

查看:164
本文介绍了在Node.js中创建模块的副本而不是实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我有关stackoverflow的第一个问题,希望一切顺利. 我一直在开发游戏(使用corona SDK),并且我使用Node.js编写了一个小型服务器来处理客户端之间的一些聊天消息,在那里没有问题. 现在,我正在努力扩展这台小型服务器,以执行更多操作,而我想做的是创建一个外部文件(模块),该文件将容纳一个对象,该对象具有表示我在其中的Room所需的所有功能和变量.我的游戏大厅"中,两个人可以进场比赛,每当我有2个球员准备比赛时,我会为他们创建一个空房间的副本,然后在该房间中初始化游戏. 所以我在主项目文件中有一个数组,其中每个单元都是一个房间,我的计划是将模块导入到该数组中,然后我可以在该特定房间"中初始化游戏,玩家可以玩,游戏将会继续,一切都会很好...但是...我在main.js中的代码:

this would be my first question ever on stackoverflow, hope this goes well. I've been working on a game (using corona SDK) and I used Node.js to write a small little server to handle some chat messages between my clients, no problems there. Now I'm working on expanding this little server to do some more, and what I was thinking to do is create an external file (module) that will hold an object that has all the functions and variables I would need to represent a Room in my games "Lobby", where 2 people can go into to play one against the other, and each time I have 2 players ready to play, I would create a copy of this empty room for them, and then initialize the game in that room. So I have an array in my main project file, where each cell is a room, and my plan was to import my module into that array, and then I can init the game in that specific "room", the players would play, the game will go on, and all would be well... but... my code in main.js:

var new_game_obj = require('./room.js');

games[room_id] = new_game_obj();

games[room_id].users = [user1_name,user2_name];

现在,在我的room.js中,我有一些东西:

Now, in my room.js, I have something of the sort:

var game_logistics = {};

game_logistics.users = new Array();

game_logistics.return_users_count = function(){
    return game_logistics.users.length;
}

module.exports = function() {
    return game_logistics;
}

到目前为止,一切都很顺利,我可以简单地进行以下操作:

So far so good, and this work just fine, I can simply go:

games[room_id].return_users_count()

然后我将得到0或1或2,具体取决于加入该会议室的用户数量. 一旦我打开一个新房间,问题就会开始,因为Node.js会实例化我创建的模块,而不是对其进行复制,即使现在我创建一个新房间,即使我删除和/或删除了旧房间,也是如此,它会包含我已经更新的旧房间中的所有信息,而不是新的洁净室中的所有信息.示例:

And I will get 0, or 1, or 2, depending of course how many users have joined this room. The problems starts once I open a new room, since Node.js will instance the module I've created and not make a copy of it, if I now create a new room, even if I eliminated and/or deleted the old room, it will have all information from the old room which I've already updated, and not a new clean room. Example:

var new_game_obj = require('./room.js');

games["room_1"] = new_game_obj();
games["room_2"] = new_game_obj();

games["room_1"].users = ["yuval","lahav"];

_log(games["room_1"].return_user_count()); //outputs 2...
_log(games["room_2"].return_user_count()); //outputs 2...

即使这样做:

var new_game_obj = require('./room.js');
games["room_1"] = new_game_obj();

var new_game_obj2 = require('./room.js');
games["room_2"] = new_game_obj2();

games["room_1"].users = ["yuval","lahav"];

_log(games["room_1"].return_user_count()); //outputs 2...
_log(games["room_2"].return_user_count()); //outputs 2...

给出相同的结果,它是我制作的所有副本"中同一模块的所有相同实例. 所以我的问题就这么简单,我如何创建原始模块的干净"副本,而不是一遍又一遍地实例化它,实际上最后只有一个凌乱的房间?

Gives the same result, it is all the same instance of the same module in all the "copies" I make of it. So my question as simple as that, how do I create a "clean" copy of my original module instead of just instancing it over and over again and actually have just one messy room in the end?

推荐答案

您正在做什么(用返回的内容替换require()调用);

What you're doing is this (replacing your require() call with what gets returned);

var new_game_obj = function() {
    return game_logistics;
}

因此,每次调用new_game_obj时,都会返回game_logistics same 实例.

So, every time you call new_game_obj, you return the same instance of game_logistics.

相反,您需要使new_game_obj返回game_logistics new 实例;

Instead, you need to make new_game_obj return a new instance of game_logistics;

// room.js
function Game_Logistics() {
    this.users = [];

    this.return_users_count = function(){
        return this.users.length;
    };
}

module.exports = function() {
    return new Game_Logistics(); 
}

这是一种观念上的转变.您会看到我们在module.exports中的Game_Logistics上使用new在每次调用Game_Logistics new 实例时.

This is quite a shift in mentality. You'll see that we're using new on Game_Logistics in module.exports to return a new instance of Game_Logistics each time it's called.

您还将看到,在Game_Logistics中,this到处都在使用,而不是Game_Logistics;这是为了确保我们引用的是正确的 instance 而不是构造函数.

You'll also see that inside Game_Logistics, this is being used everywhere rather than Game_Logistics; this is to make sure we're referencing the correct instance of Game_Logistics rather than the constructor function.

我还大写了您的game_logistics函数,以遵循广泛使用的命名约定,即构造函数应大写(更多信息).

I've also capitalized your game_logistics function to adhere to the widely-followed naming convention that constructor functions should be capitalized (more info).

当您使用多个函数实例时,建议利用JavaScript中的原型链.您可以阅读有关"JavaScript原型继承*"的各种文章(例如 ),但目前,以上已可以满足您的需求.

Taking advantage of the prototype chain in JavaScript is recommended when you're working with multiple instances of functions. You can peruse various articles on "javascript prototypical inheritance* (e.g. this one), but for now, the above will accomplish what you need.

这篇关于在Node.js中创建模块的副本而不是实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆