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 May 16th, 2005, 01:37 PM
Registered User
Join Date: May 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Private members in constructors

I'm confused about private members in Javascript. According to this: http://www.crockford.com/javascript/private.html private members are possible. However, many developers simply indicate an intended private member with an underscore convention due to (perceived?) lack of private scope. (Chap3, page 88)

So would the following modification of your code using crockford's pattern be a valid way to introduce private members? Thanks!

function Car(sColor, iDoors, iMpg) {
    var self = this;
    var color = sColor;
    var doors = iDoors;
    var mpg = iMpg;
    var drivers = new Array("Mike", "Sue");

    if (typeof Car._initialized == "undefined") {
      Car.prototype.showColor = function () {

      Car.prototype.showDoors = function () {

      Car.prototype.showMpg = function () {

      Car.prototype.showDrivers = function () {

      Car._initialized = true;

var oCar = new Car("red", "4", "30mpg");

alert(oCar.color); //undefined
alert(oCar.doors); //undefined
alert(oCar.mpg); //undefined
alert(oCar.drivers); //undefined

Old May 16th, 2005, 02:32 PM
nzakas's Avatar
Wrox Author
Join Date: Dec 2004
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts

Technically, there is no way to create private members in JavaScript. What Crockford suggests is actually a way of using closures (p. 65) to "capture" the locally defined variables in functions. To prove this, try creating a second object after the first, like this:

var oCar2 = new Car("blue", "2", "20mpg");

alert(oCar2.color); //undefined
alert(oCar2.doors); //undefined
alert(oCar2.mpg); //undefined
alert(oCar2.drivers); //undefined

oCar2.showColor();  //red
oCar2.showDoors();  //4
oCar2.showMpg();    //20mpg
Because the values were only captured in the closure the first time the Car() constructor was called, those variables are exactly the same as when the first instance was created.

You can use Crockford's methodology directly as it uses the constructor paradigm and so the variable captures occur correctly, you just can't mix it with dynamic prototyping.

Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
Old May 16th, 2005, 03:03 PM
Registered User
Join Date: May 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts

You're absolutely right. And I figured this out a few minutes ago by instantiating a second object, as you have.

Thanks for clearing this up.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Constructors madhuriguda C# 2 April 29th, 2008 08:11 AM
Constructors - instantiation brysora C# 2005 1 September 3rd, 2007 08:45 PM
Cascading Constructors rodmcleay General .NET 4 May 18th, 2007 03:57 AM
C++ classes and private members steuckcl Visual C++ 1 November 2nd, 2005 05:30 PM
Static Constructors Ibn_Aziz C# 3 December 25th, 2003 04:07 AM

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