Wrox Programmer Forums
|
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
 
Old December 3rd, 2006, 09:26 PM
Registered User
 
Join Date: Dec 2006
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
 
Old December 4th, 2006, 02:07 AM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
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/
 
Old December 4th, 2006, 09:10 AM
Registered User
 
Join Date: Dec 2006
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







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





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