2013-02-27 186 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

相關問題