# Using the prototype function with Homemade Objects

**URL:** <https://forum.kirupa.com/t/using-the-prototype-function-with-homemade-objects/63263>\
**Category:** flash\
**Created:** [September 4, 2004, 4:17pm UTC](https://forum.kirupa.com/t/using-the-prototype-function-with-homemade-objects/63263 "2004-09-04T16:17:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pup\_100](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@Pup\_100](https://forum.kirupa.com/u/Pup_100)\
**Post date:** [September 4, 2004, 4:17pm UTC](https://forum.kirupa.com/t/using-the-prototype-function-with-homemade-objects/63263/1 "2004-09-04T16:17:06Z")

</div>

[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]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 5, 2004, 5:53am UTC](https://forum.kirupa.com/t/using-the-prototype-function-with-homemade-objects/63263/2 "2004-09-05T05:53:46Z")

</div>

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.

```auto
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? 😛

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 5, 2004, 6:02am UTC](https://forum.kirupa.com/t/using-the-prototype-function-with-homemade-objects/63263/3 "2004-09-05T06:02:30Z")

</div>

Your making all the sense I need Kode!!

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 5, 2004, 6:04am UTC](https://forum.kirupa.com/t/using-the-prototype-function-with-homemade-objects/63263/4 "2004-09-05T06:04:03Z")

</div>

Good, glad I could help. 😃
