Using the prototype function with Homemade Objects

[font=Times New Roman]On doing a bit of work on “Homemade Objects” from examples provided in a reference book. I’m a little bit confused as to what the “prototype” function actually does.[/font]

[font=Times New Roman]According to the book if you have created two separate homemade objects (CD & BankAct) & then use the following code:[/font]

[font=Times New Roman][color=red]CD.prototype=new BankAct();[/color][/font]

[font=Times New Roman]CD will inherit ALL the “methods & properties” of BankAct, but if I run the following script, it would appear that none of the “Properties” have made it across into the “test” (New CD) object.[/font]

[font=Times New Roman][color=red]function BankAct(startingBalance,interestRate){[/color][/font]

[font=Times New Roman][color=red]this.balance=startingBalance;[/color][/font]

[font=Times New Roman][color=red]this.rate=interestRate;[/color][/font]

[font=Times New Roman][color=red]}[/color][/font]

[font=Times New Roman][color=red]function multiplyAndAdd(){[/color][/font]

[font=Times New Roman][color=red]this.balance+=(this.balance*this.rate);[/color][/font]

[font=Times New Roman][color=red]}[/color][/font]

[font=Times New Roman][color=red]BankAct.prototype.compound=multiplyAndAdd;[/color][/font]

[font=Times New Roman][color=red]function CD(startingBalance,interestRate,lengthOfTermInDays){[/color][/font]

[font=Times New Roman][color=red]this.term=lengthOfTermInDays;[/color][/font]

[font=Times New Roman][color=red]var now=new Date();[/color][/font]

[font=Times New Roman][color=red]this.renewalDate=new Date();[/color][/font]

[font=Times New Roman][color=red]this.renewalDate.setDate(now.getDate()+this.term);[/color][/font]

[font=Times New Roman][color=red]this.longDate=this.renewalDate.toString();[/color][/font]

[font=Times New Roman][color=red]}[/color][/font]

[font=Times New Roman][color=red]CD.prototype=new BankAct();[/color][/font]

[font=Times New Roman][color=red]function extendDate(){[/color][/font]

[font=Times New Roman][color=red]this.renewalDate.setDate(this.renewalDate.getDate()+this.term);[/color][/font]

[font=Times New Roman][color=red]this.longDate=this.renewalDate.toString();[/color][/font]

[font=Times New Roman][color=red]}[/color][/font]

[font=Times New Roman][color=red]CD.prototype.renew=extendDate;[/color][/font]

[font=Times New Roman][color=red]test=new CD(100,.1,30);[/color][/font]

[font=Times New Roman][color=red]trace("init: " + test.balance + " "+test.longDate);[/color][/font]

[font=Times New Roman][color=red]test.compound();[/color][/font]

[font=Times New Roman][color=red]trace ("after compound: " + test.balance + " "+ test.longDate);[/color][/font]

[font=Times New Roman][color=red]test.renew();[/color][/font]

[font=Times New Roman][color=red]trace ("after renew: " + test.balance + " "+test.longDate);[/color][/font]

[font=Times New Roman]In other words unless I specify in it’s constructor that CD has got properties for “balance” & “rate” contrary to what the book says CD will not inherit them, as when I test the movie the “compound()”function shows 0 for the “After compound” amount?[/font]

[font=Times New Roman]So I suppose the bottom line is; does the prototype function transfer in both methods & properties?[/font]

[font=Times New Roman]Any help please. [/font]

The object does inherit those properties. However, you’re defining their values through the parameters passed to the constructor function, but you’re not passing any parameters when you define the [font=courier new]prototype[/font] property of the class; therefore their values will be [font=courier new]undefined[/font].

Solution: set the values of those properties within the constructor of the “CD” class.

function CD(startingBalance, interestRate, lengthOfTermInDays) {
	this.balance = startingBalance;
	this.rate = interestRate;
	this.term = lengthOfTermInDays;
	var now = new Date();
	this.renewalDate = new Date();
	this.renewalDate.setDate(now.getDate()+this.term);
	this.longDate = this.renewalDate.toString();
}

…Am I making any sense? :stuck_out_tongue:

Your making all the sense I need Kode!!

That’s exactly what I needed to know - Thank you very much for your help :slight_smile:

Good, glad I could help. :smiley: