Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | 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 Display Modes
  #1 (permalink)  
Old May 16th, 2005, 02:37 PM
Registered User
 
Join Date: May 2005
Location: , , .
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!


Code:
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 () {
          alert(color);
      };

      Car.prototype.showDoors = function () {
          alert(doors);
      };

      Car.prototype.showMpg = function () {
          alert(mpg);
      };

      Car.prototype.showDrivers = function () {
          alert(drivers);
      };

      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

oCar.showColor();
oCar.showDoors();
oCar.showMpg();
oCar.showDrivers();
Reply With Quote
  #2 (permalink)  
Old May 16th, 2005, 03:32 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

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:

Code:
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
oCar2.showDrivers();
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)
http://www.nczonline.net/
Reply With Quote
  #3 (permalink)  
Old May 16th, 2005, 04:03 PM
Registered User
 
Join Date: May 2005
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

Reply With Quote
Reply


Thread Tools
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
Constructors madhuriguda C# 2 April 29th, 2008 09:11 AM
Constructors - instantiation brysora C# 2005 1 September 3rd, 2007 09:45 PM
Cascading Constructors rodmcleay General .NET 4 May 18th, 2007 04: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



All times are GMT -4. The time now is 07:48 AM.


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