英文:
Javascript problem with object cloning and eval()
问题
以下是翻译好的部分:
我正在编写一个小游戏,使用socketio和nodejs服务器,对我来说还很新颖。当有人创建一个游戏时,它会将信息发送到服务器,服务器会创建一个包含所有游戏信息的对象。我使用eval()来使对象以游戏名称命名,例如"bob_game = {...}"。
这是我的代码:
games.push("Partie de " + data.pseudo);
gamesStatus.push("lobby");
eval("var " + data.pseudo + "_game = {name :'" + data.pseudo + "',status : 2,playerNb : 1,maxPlayerNb :" + data.nbPlayers + ",players : ['" + data.pseudo + "'], xCoords : [], yCoords : [], directions : []}");
io.emit(data.pseudo, {command : "joinGame", game : data.pseudo});
io.emit("updateGameList", {command : "updateGameList", game : games, gameStatus : gamesStatus});
这一切都正常工作,我能够向其他用户发送所有信息,告诉他们已创建游戏!
在网站上,每个可加入的游戏都有一个按钮。当您按下其中一个按钮时,它会向服务器发送一个带有游戏ID和玩家名称的joinGameRequest。所有信息都被正确收集,我能够设置一个临时对象来替代bob_game对象:
game = eval(gameName + "_game");
这样可以避免在想要进行更改时调用原始对象bob_game,否则我将到处放置eval(),因为bob并不总是叫bob...
但是,我知道在对"game"进行的所有更改之后,我必须将所有属性传递回bob_game"以某种方式保存一切"。这就是我遇到问题的地方...它说game未定义...尽管我在上面的几行中已经进行了更改,我能够记录game的playerNb...所以game已经被定义...
console.log(game.playerNb);
eval(gameName + "_game = game");
我需要帮助,请xD
英文:
I'm programming a little game with socketio and nodejs server and that is quite new for me. When someone creates a game it sends to the server the info and the server creates an object with all the game info. I'm using the eval() to make the object named after the game-name like "bob_game = {...}".
Here is my code :
games.push("Partie de " + data.pseudo);
gamesStatus.push("lobby");
eval("var " + data.pseudo + "_game = {name :'" + data.pseudo + "',status : 2,playerNb : 1,maxPlayerNb :" + data.nbPlayers + ",players : ['" + data.pseudo + "'], xCoords : [], yCoords : [], directions : []}");
io.emit(data.pseudo, {command : "joinGame", game : data.pseudo});
io.emit("updateGameList", {command : "updateGameList", game : games, gameStatus : gamesStatus});
This all works correctly and i'm able to send back all the info to other users saying a game has been created !
On the website their is a button for every joinable game. When you press one of them, it sends to the server a joinGameRequest with game id and the player's name. All the info is collected correctly and i'm able to set a temporary object replacing the bob_game object :
game = eval(gameName + "_game");
This avoids me calling the original object, bob_game, when I want to make a change otherwise I would put eval() everywhere cause bob isn't always called bob...
But, I know that after all the changes made to "game", I have to transfer all the propeties back to bob_game "to save everything in a way". That is where I'm having a problem ... I says game is not defined ... Even though i've been making changes few lines above and I'm able to log the playerNb of game ... So game is defined ...
console.log(game.playerNb);
eval(gameName + "_game = game");
I need help pls xD
答案1
得分: 2
不要这样做:
eval("var " + data.pseudo + "_game = {
如果你需要一个名为'bob_game'的东西,将它放在一个对象内:
var games = {};
var newGameKey = data.pseudo + "_game";
games[newGameKey] = {...};
然后你可以随时通过这个键来访问它。
英文:
don't do this:
eval("var " + data.pseudo + "_game = {
If you need to have something called 'bob_game', put it inside of an object
var games = {};
var newGameKey = data.pseudo + "_game";
games[newGameKey] = {...};
Then you can always access it by that key.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论