473,506 Members | 17,393 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

David Crockford's private variables not working in IE6sp1?

3 New Member
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
5 2262
gits
5,390 Recognized Expert Moderator Expert
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
fourpastmidnight
3 New Member
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
5,390 Recognized Expert Moderator Expert
i think you are right now ;) after a first look over ...

kind regards ...
Jun 26 '07 #4
acoder
16,027 Recognized Expert Moderator MVP
BTW, how do you get syntax coloring in these forums??
In your code tag, add =javascript, e.g. [code=javascript]
Jun 27 '07 #5
fourpastmidnight
3 New Member
Thanks acoder!!! I tried [ CODE javascript ] but not the other way!

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

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

Similar topics

2
12953
by: Andy Fish | last post by:
Hi, I am in the process of designing a UI which has to be fairly sophisticated. There will be a number of list boxes and other controls, with pop-up windows to edit certain properties. It's the...
8
2281
by: Radu Colceriu | last post by:
HI, I've an asp.net app like this: login.aspx (no frame) :- save in session the user and pass -> framedoc.html :- frameset 2 content 1. menu.aspx...
1
23103
by: Edward | last post by:
I have trouble with some of the concepts of OOP, and am struggling currently with Private Shared Functions. I think I understand Private (not available outside the class). I think I understand...
2
3974
by: Rob Long | last post by:
Hi there Is there any way to access private variables directly from within a priviliged function? I have a situation where the priviliged function's execution context contains variables of the...
86
4554
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
2
2486
by: Peter Michaux | last post by:
Douglas Crockford doesn't seem to like JavaScript's built-in syntax for building new objects based on a prototype object. The constructor function, its prototype property and the "new" keyword all...
76
3964
by: lorlarz | last post by:
Crockford's JavaScript, The Good Parts (a book review). This shall perhaps be the world's shortest book review (for one of the world's shortests books). I like Douglas Crockford (because I am a...
8
2658
by: Martin Rinehart | last post by:
The Dojo Style Guide suggests prepending an underscore to indicate a "private" variable. Crockford says don't; JavaScript doesn't have privates. Which should be the convention? I'll vote first:...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.