Domanda

I have an account class in javascript. That is my parent class. depositaccout and savingsacount are the children. all this classes are in external javascript files. the calsses are:

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);
};

In my html page I have another script and I'm trying to initialize a deposit account, meaning I want to create an instance of the account chile- a deposit account. I'm getting an an uncought error for the call method in deposit account class.

Can I get help with that? What am I doing wrong? The html script:

<script> 
var account = new account(232, "young");
var deposit = new depositaccount(232, "young", 1000, 2555);
</script>
È stato utile?

Soluzione

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

You are replacing the account function with the object of account function.

Suggestion:

Its a convention which JavaScript programmers follow, using initial caps for the function names.

Altri suggerimenti

You might want to use the Mixin pattern here, it's a really useful design pattern for problems like yours.

EDIT: Forget mixin, it would work but a different way, here's a closer match to your problem with sub classing.

e.g.

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');
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top