2014-01-16 83 views
3

我在javascript中有一個帳戶類。那是我的父母班。 depositaccout和savingsacount是孩子。所有這些類都在外部JavaScript文件中。在calsses是:繼承與javascript

function account(accountNum, type) 
{ 
    this.accountNum = accountNum; 
    this.type = type; 
} 

function depositAccount(accountNum,type, balance, credit) 
{ 


    this.balance = balance; 
    this.credit = credit; 
    account.call(this, accountNum,type); 
}; 


function savingAccount(accountNum,type, amount, yearlyPrime) 
{ 

    this.amount = amount; 
    this.yearlyPrime = yearlyPrime; 
    account.call(this, accountNum, type); 
}; 

在我的html頁面我有另一個劇本,我想初始化一個存款賬戶,這意味着我要創建的帳戶的一個實例chile-一個存款賬戶。在存款賬戶類中,我收到了一個無用的錯誤。

我可以獲得幫助嗎?我究竟做錯了什麼? 的HTML腳本:

<script> 
var account = new account(232, "young"); 
var deposit = new depositaccount(232, "young", 1000, 2555); 
</script> 
+1

不確定你是否在這裏犯了一個錯字,但不應該第二個'new account()'是'new depositAccount()'?使用你提供的代碼,你會傳遞太多的參數。 –

+0

這是一個錯字。仍然沒有去這裏。 – user2674835

回答

4
var account = new account(232, "young"); 

您與account功能的對象替換account功能。

建議:

其其JavaScript程序員遵循,使用首字母大寫的函數名的約定。

+0

我不能相信它就是這麼簡單!謝謝 ! – user2674835

+1

@ user2674835如果這個答案對您有幫助,不要忘記標記爲已接受。 –

+0

我會在3分鐘內:) – user2674835

0

你可能想在這裏使用密新模式,它是像你這樣的問題,一個真正有用的設計模式。

編輯:忘了mixin,它會工作,但不同的方式,這是一個更接近你的問題與子分類匹配。

例如

var Account = function(accountNum, type) { 
    this.accountNum = accountNum; 
    this.type = type; 
} 

var DepositAccount = function(accountNum, balance, credit) { 
    Account.call(this, accountNum, 'deposit'); 
    this.balance = balance; 
    this.credit = credit; 
}; 

DepositAccount.prototype = Object.create(Account.prototype); 
var myAccount = new DepositAccount('12345', '100.00', '10'); 
+0

我不明白你的建議 – user2674835

+0

讓我更新這個例子,這不是最好的 –