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
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 3rd, 2006, 09:26 PM
Registered User
 
Join Date: Dec 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic Prototyping with Inheritance

Hi,

The section on dynamic prototyping with inheritance (pages 114 and 115) emphatically states that the hybrid constructor/prototype inheritance paradigm does not work with dynamic prototyping.

That's not true - I've come up with a technique which allows one to implement dynamic prototyping with inheritance.

The example given on page 115 can be rewritten as:

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

        if (typeof Triangle._initialized == 'undefined') {
            delete this;
            Triangle.prototype = new Polygon;
            Triangle.prototype.getArea = function () {
                return 0.5 * this.base * this.height;
            };
            Triangle._initialized = true;
            var newTriangle = new Triangle(iBase, iHeight);
            return newTriangle;
        }
    }

This encapsulates all the code in the constructor as required (the lines I've added to the code in the book are highlighted in red).

This section of the text is therefore quite misleading and maybe Nicholas should think about reworking it for the next edition.

Best Regards,

Sajid
Reply With Quote
  #2 (permalink)  
Old December 4th, 2006, 02:07 AM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

That's an interesting approach, however, it is not the equivalent of the hybrid constructor/prototype inheritance approach. First,
Code:
delete this
doesn't actually do anything. The delete operator is used only to delete properties on an object, not the object itself. Second, you are creating a second object the first time the constructor is called, which forces the developer to effectively call the constructor twice instead of once.

Thanks for the feedback, but I don't believe you've disproved my statement.


Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
Reply With Quote
  #3 (permalink)  
Old December 4th, 2006, 09:10 AM
Registered User
 
Join Date: Dec 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oops ! I see now that the garbage collector would destroy the first object automatically, so the delete statement wouldn't be required even if it actually destroyed the object (which it doesn't, as you point out).

However, I don't understand why calling the constructor twice is a problem ?

Sajid


Reply With Quote
Reply


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
Inheritance magagulad Visual Basic 2005 Basics 1 May 12th, 2008 07:51 AM
Is it possible via Inheritance!!! mike_remember ASP.NET 2.0 Professional 0 November 26th, 2007 10:25 AM
Inheritance michaelcode ASP.NET 2.0 Basics 5 September 26th, 2006 01:40 PM
Need help with inheritance filip BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 August 25th, 2006 09:38 PM



All times are GMT -4. The time now is 09:03 AM.


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