By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,474 Members | 1,283 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,474 IT Pros & Developers. It's quick & easy.

David Crockford's private variables not working in IE6sp1?

P: 3
Ok, before anyone gets on me ( ;) ), I'm developing on Windows 2000 with IE6sp1 (that's what my company uses). So no, I can't use Firefox, though I wish I could. Ok, with that out of the way, here's the problem.

I read David Crockford's way to create private member variables. I created a namespace. For our sake, let's call it ns.
Expand|Select|Wrap|Line Numbers
  1. ns = { };
Now, I create a "class" within this namespace and create some local (read private member) variables.
Expand|Select|Wrap|Line Numbers
  1. ns.MyClass = function ( ) { var privateVar; }
Now I want to create a VB/C# like property accessor (not a separate get/set method), so I code something like:
Expand|Select|Wrap|Line Numbers
  1. ns.MyClass.prototype.myProperty = function( value ) {
  2. if (arugments.length == 1)
  3.     this.privateVar = value;
  4. else if (arguments.length == 0)
  5.     return this.privateVar;
  6. else
  7.     throw new Error("myProperty expects 0 or 1 arguments: value");
  8. }
Now, in my web page, I would have the following (assume I've included the script in the page or an extermal module...):
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.     The value of my property is:
  4.  
  5.     <script>
  6.      var myClass = new ns.MyClass();
  7.      myClass.privateVar = "Hello World!"; // Shouldn't work, but does??
  8.      document.write(myClass.privateVar + "<br /><br />"); // Shouldn't work but does??
  9.  
  10.      myClass.myProperty("Hello World!"); // Works, as it should??
  11.      document.wirte(myClass.myProperty() + "<br /><br />"); // Works, as it should??
  12.     </script>
  13. </body>
  14. </html>
  15.  
When I run code similar to this in IE6sp1, I can directly access myClass.privateVar. This shouldn't be so, should it? Please help, as I'm trying to enforce encapsulation. In addition, I will be creating several "classes" under one namespace. So for instance,
Expand|Select|Wrap|Line Numbers
  1. ns = { };
  2. ns.Class1 = function ( ) { /* some class code */ }
  3. ns.Class2 = function(init) { /* some class code */}
  4.  
Therefore, Class1 has some private members (ideally) that should not be directly accessible by Class2. From what I've written above, it would seem to me that this kind of private data hiding is not possible as outlined at David Crockford's site, unless I have misunderstood or coded something.

Thanks in advance for your help!
Jun 26 '07 #1
Share this Question
Share on Google+
5 Replies


gits
Expert Mod 5K+
P: 5,390
hi,

with:

Expand|Select|Wrap|Line Numbers
  1. myClass.privateVar = "Hello World!"
you create a new public property named privateVar of the instance of myClass and set its value to 'Hello World'. a private var is only available to the constructor itself ... everything you do with this.var_name is public ... read the private section carefully ... its explained there real good ...

Expand|Select|Wrap|Line Numbers
  1. // what you did is similar to the following
  2. var obj = {};
  3. obj.var_name = 'value';
  4.  
kind regards ...
Jun 26 '07 #2

P: 3
Hehe,

You are right! I was too close and couldn't see the forest for the trees. I come from a "class"-ical inheritance background, so this is a very different way of thinking for me!

But then I still have a question: how do I know if I have accidentally created a public instance Class1 object variable versus using my "private" Class1 instance variable. For instance:

Expand|Select|Wrap|Line Numbers
  1. var ns = { }; 
  2. ns.Class1 = function( ) {
  3. var privateVar;
  4. var self = this;
  5.  
  6. this.getPrivateVar = function( ) { 
  7. /* If I understand correctly, the below is wrong....
  8. return this.privateVar; // Creates a new variable belonging to getPrivateVar and returns its value (which is 'undefined')
  9. */
  10.  
  11. /* It should be the following: */
  12. return self.privateVar; // Now we are returning Class1's privateVar.
  13. }
  14.  
  15. this.setPrivateVar = function( privateVar ) {
  16. /* For simplicity's sake...assume error checking has been performed. */
  17.  
  18. /* Again, this is incorrect if I understand correctly, and is really
  19. * creating a variable ns.Class1.setPrivateVar.privateVar to
  20. * some value privateVar.
  21. */
  22. this.privateVar = privateVar;
  23.  
  24. /* It should really be the following: */
  25. self.privateVar = privateVar;
  26. }
  27.  
  28. ns.Class1.prototype.PrivateVar = function( privateVar ) {
  29. if (arguments.length == 0 ) return this.getPrivateVar( );
  30. else if (arguments.length == 1) this.setPrivateVar(privateVar);
  31. else throw new Error("ns.Class1.PrivateVar expects 0 or 1 arguments: privateVar");
  32. }
  33.  
Now, if I instantiate this class and do the following:
Expand|Select|Wrap|Line Numbers
  1. var myObject = new ns.Class1( );
  2.  
  3. myObject.privateVar = 1; // Creates a new, public, instance variable privateVar
  4. myObject.PrivateVar(1); // Sets the private ns.Class1.privateVar instance variable.
  5. document.write(myObject.privateVar); // Writes the public instance variable privateVar to the HTML document.
  6. document.write(myObject.PrivateVar()); // Writes the private instance variable ns.Class1.privateVar to the document.
  7.  
Is my understanding correct? BTW, how do you get syntax coloring in these forums??
Jun 26 '07 #3

gits
Expert Mod 5K+
P: 5,390
i think you are right now ;) after a first look over ...

kind regards ...
Jun 26 '07 #4

acoder
Expert Mod 15k+
P: 16,027
BTW, how do you get syntax coloring in these forums??
In your code tag, add =javascript, e.g. [code=javascript]
Jun 27 '07 #5

P: 3
Thanks acoder!!! I tried [ CODE javascript ] but not the other way!

And Thanks gits for all of your help!
Jun 27 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.