F
F
fanhypermax2018-04-25 17:59:45
JavaScript
fanhypermax, 2018-04-25 17:59:45

How to create a personal object for each user?

How to do this example so that the new object is not written to Users = [] but is separate for each user. What would not be stored in a common array.
/index

var express = require('express')
  , app = express()
  , path = require('path')
  , server = require('http').createServer(app)
  , io = require('socket.io')(server)
  , obj = require("./user");
   
server.listen(80,()=> {
  
  var Users = [];

  for(user of [{id:1,name:'Вася'},{id:2,name:'Петя'}]){
  
          Users[user.id] = new obj(user);
           
    Users[user.id].Say('Привет');
       
  }	   
       
  io.on('connection',(socket) => {
    
    socket.on('set name',(user, res) => {

      Users[user.id].Name =user.name;
        
    });

  });
  
});

app.use(express.static(path.join(__dirname, 'publuc')));

/user
module.exports = function (user) {

  this.Id = user.id;
  this.Name = user.name;
  
  this.Say=(word)=> {
  
     console.log(this.Name+':'+word);
     
  };
  
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-04-26
@Minifets

What would the new object not be written to Users = [] but for each user a separate one.

And how do you imagine that? For each user to get a variable with a unique name? :).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question