2013-02-27 126 views
0
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); 
} 

第一个函数是构造函数。假设'username','password'是用户输入的,我想用USER输入的名字创建一个账户对象。 (如在对象中将是用户输入的'用户名')。 我知道为什么我所做的不起作用,但我不知道如何实际完成。 在此先感谢!构造函数对象名称 - javascript

对不起,用户输入用户名,密码和电子邮件。密码和电子邮件只是对象'账户'中的两个属性。用户名是我想要的对象本身。

+1

这真的不清楚你在问什么。在一个地方你谈论'username'和'password',但在另一个地方你使用'password'和'email' ......? – 2013-02-27 15:32:15

回答

5

听起来像你想要一个对象的键是用户名?

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

像这样:

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

然后:

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

即使它与这个问题有任何关系,你为什么要在这个函数中修改构造函数呢?整个内部功能似乎是多余的。 – 2013-02-27 15:33:22

相关问题