简体   繁体   中英

Constructor object name - javascript

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

function createAccount()
{
    var username="Moshiko22";
    var password="1112"
    var email="moshiko@walla.co.il";
    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.

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

var users = {};
function createAccount()
{
    var username="Moshiko22";
    var password="1112"
    var email="moshiko@walla.co.il";
    users[username] = new Account(password, email);
}

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", "myusername@mydomain.com");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM