Javascript使用这样的数据结构创建对象

时间:2014-06-17 18:44:18

标签: javascript node.js object

如果可能,我想创建一个具有类似数据结构的对象。 我必须为每个玩家创建一个新对象吗?有人可以告诉我怎么样?

players
players.name='John'

players.John.age='12'
players.John.adress='London ..'
players.John.telnumber='09876587655'

EDIT1

抱歉,我知道这是基本的。我只问一个问题,我会尝试学习更好的javascript。我需要将存储在“event”中的数据传递给object。“event”.id(就像players.John.id而不是players.event.id) 抱歉我的英语不好。

// in app.js
var fs = require('fs');
var socketio = require('socket.io');
Tail = require('tail').Tail;
var express = require('express');
var http = require('http');
var colors = require('colors');
var app = express()
  , server = require('http').createServer(app)
  , io = socketio.listen(server);             // socket needs to listen on http server

server.listen(9099);



app.use(express.static(__dirname + '/public'));

var port = process.env.PORT || 3000;

app.listen(port, function() {
    console.log('\r\n');
    console.log("Express listening on port " + port +'.'.green);
});

// Routing
//app.use(express.static(__dirname));

// usernames which are currently connected to the chat
//var players = [];
var players = {};


io.sockets.on('connection', function(socket) {
    // do all of your socket work in here
    console.log('\r\n');
    console.log("Connection".green);

    var sessionid = socket.id;
    console.log(sessionid);

    // Success!  Now listen to messages to be received
    socket.on('message',function(event){ 
        console.log('Received message:',event);
    });

    socket.on('add user',function(event){ 
        console.log('New User:',event);
        // we store the username in the socket session for this client

        socket.username = event;
        // add the client's username to the global list
        players.event = {};
        players.event.id = sessionid;
        //players.John.foo = "yeah"
        //players.John.name = "John"
        console.log(players);

        socket.emit('login', {});
        // echo globally (all clients) that a person has connected

        socket.broadcast.emit('user joined', {
            username: socket.username
        });
    });

    //socket.emit('start', 'newround');     
});

EDIT2

搞定了。

// in app.js
var fs = require('fs');
var socketio = require('socket.io');
Tail = require('tail').Tail;
var express = require('express');
var http = require('http');
var colors = require('colors');
var app = express()
  , server = require('http').createServer(app)
  , io = socketio.listen(server);             // socket needs to listen on http server

server.listen(9099);

app.use(express.static(__dirname + '/public'));

var port = process.env.PORT || 3000;

app.listen(port, function() {
    console.log('\r\n');
    console.log("Express listening on port " + port +'.'.green);
});

// Routing
//app.use(express.static(__dirname));

// usernames which are currently connected to the chat
//var players = [];
var players = {};

io.sockets.on('connection', function(socket) {
    // do all of your socket work in here
    console.log('\r\n');
    console.log("Connection".green);
    var sessionid = socket.id;
    console.log(sessionid);

    // Success!  Now listen to messages to be received
    socket.on('message',function(event){ 
        console.log('Received message:',event);
    });

    socket.on('add user',function(event){ 
        console.log('New User:',event);
        // we store the username in the socket session for this client

        socket.username = event;
        // add the client's username to the global list
        players[event] = {};
        players[event].id = sessionid;
        //players.John.foo = "yeah"
        //players.John.name = "John"
        console.log(players);

        socket.emit('login', {});

        // echo globally (all clients) that a person has connected
        socket.broadcast.emit('user joined', {
            username: socket.username
        });
    });

    //socket.emit('start', 'newround');
});

2 个答案:

答案 0 :(得分:5)

您正在寻找一个players对象,其名称引用了各个玩家。所以:

var players = {};
players['John'] = {  
  'age' = 12,
  'address' = 'London...',
  'telnumber' = '09876587655'
};

您也可以将“John”作为players.John访问,但如果任何名称包含空格等,则会变得棘手。

同样,玩家属性可以通过以下方式访问:

players.John['age'] = 13;   

players.John.age = 13;

答案 1 :(得分:2)

var name = "John";
var players = {};
players[name] = {};
players[name].age = '12';
players[name].address = "address";
players[name].telnumber = "tel";