Wrox Programmer Forums
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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?

  #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)

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

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