Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers by Nicholas C. Zakas; ISBN: 9780764579080
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 17th, 2006, 09:25 AM
Registered User
Join Date: Aug 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Inheritance issues

Let's consider this example (p.112-113)

function Polygon(iSides) {
    this.sides = iSides;

Polygon.prototype.getArea = function () {
    return 0;

function Triangle(iBase, iHeight) {
    Polygon.call(this, 3);
    this.base = iBase;
    this.height = iHeight;

Triangle.prototype = new Polygon();
Triangle.prototype.getArea = function () {
    return 0.5 * this.base * this.height;

Here are my questions:

1/ "Triangle.prototype = new Polygon();" : Doesn't this line overwrite all the defined things in the Triangle function?

2/ "Triangle.prototype = new Polygon();" : Won't this same line assign this.sides to an empty string or undefined if one considers that the polygon function has no arguments? Or does writing ...prototype = new SomeClass() only "touch on" the methods?

Thank you and try to be as compete as possible?

Reply With Quote
  #2 (permalink)  
Old August 25th, 2006, 09:46 PM
nzakas's Avatar
Wrox Author
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts

1) No it doesn't. The things defined in the Triangle() function are applied after the changes to the prototype object. It's a little bit confusing because the code is arranged the way it is, but keep in mind that changes to the prototype take effect immediately while changes inside of a function take effect only when the function is called.

2) This will assign sides to be equal to undefined. You don't want to pass in any default values because you want your constructor, Triangle, to supply them.

Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
connection string issues, web.config file issues kaliaparijat ASP.NET 2.0 Professional 1 June 12th, 2008 08:07 AM
Is it possible via Inheritance!!! mike_remember ASP.NET 2.0 Professional 0 November 26th, 2007 10:25 AM
Inheritance abhi_bth ASP.NET 1.0 and 1.1 Basics 0 September 23rd, 2006 10:03 AM
c# inheritance bhohman C# 2 March 26th, 2004 01:47 PM

All times are GMT -4. The time now is 06:28 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.