JavaScript中的对象克隆和eval()问题。

huangapple go评论75阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年1月3日 19:56:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定