frrlod
frrlod

Reputation: 6685

Constructor object name - javascript

function Account(password, email)
{
    this.password=password;
    this.email=email;
}

function createAccount()
{
    var username="Moshiko22";
    var password="1112"
    var email="[email protected]";
    username=new Account(password, email);
}

The first function is a constructor. Assuming 'username', 'password' are user entered, I want to create an account object with the name the USER entered. (as in the object would be the 'username' the user has entered). I know why what I've done doesn't work, but I don't know how to actually get it done. Thanks in advance!

Sorry for being unclear: the user enters username, password and email. the password and email are just 2 properties in the object 'Account'. The username is what I want as the object itself.

Upvotes: 0

Views: 165

Answers (2)

d4rkpr1nc3
d4rkpr1nc3

Reputation: 1827

Like this:

function Account(password, email)
{
    this.constructor = function (password, email) {
        this.password=password;
        this.email=email;
    }.apply(this, arguments);
}

And then :

var account = new Account("mypwd", "[email protected]");

Upvotes: -1

epascarello
epascarello

Reputation: 207511

Sounds like you want an object with the keys being the username?

var users = {};
function createAccount()
{
    var username="Moshiko22";
    var password="1112"
    var email="[email protected]";
    users[username] = new Account(password, email);
}

Upvotes: 5

Related Questions