0

I'm trying to create a Website where if you enter information it makes an object user. But I don't know how I would make different object names for each one. Is there a way you could create a variable name from a string?

function user(name,real){
  this.username = name,
  this.realname = real,
  this.id = Math.floor(Math.random() *1000);
  this.friends = 0,
  this.friendnames = [],
  this.listuser = function() {
    return this.username;
  }
};
function newuser(name, email,username){
//I  want name to be what the user enters instead of name.
  name = new user(username,name)
};

what should I do?

haha2567
  • 7
  • 2

1 Answers1

1

I mean you can to create users object and use it in next way:

let users = {};

function user(name,real){
  this.username = name,
  this.realname = real,
  this.id = Math.floor(Math.random() *1000);
  this.friends = 0,
  this.friendnames = [],
  this.listuser = function() {
    return this.username;
  }
};
function newuser(name, email,username){
  users[name] = new user(username, name);
};

newuser('name1', 'name1@mail.com', 'username1');

console.log(users['name1'].listuser());
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39