3

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>
2
  • 1
    Not sure if you've made a typo here, but shouldn't the second new account() be new depositAccount()? With the code you've provided you're passing in too many arguments otherwise. Commented Jan 16, 2014 at 10:37
  • It was a typo. Still no go here. Commented Jan 16, 2014 at 10:40

2 Answers 2

4
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.

Sign up to request clarification or add additional context in comments.

2 Comments

I can't beleive it was that simple! Thank you !
I will, in 3 minutes :)
0

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

1 Comment

I don't understand what you're suggesting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.