473,659 Members | 2,645 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.private Var. 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 2270
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
12965
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 kind of thing I would normally have done in VB but I want it to be browser-based. I've only used javascript for trivial things before so this would be my first serious javascript development. I would like it to run on all reasonably recent...
8
2285
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 2.docviewver.aspx
1
23189
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 Shared (available without having to instantiate a class). So how could a Private Shared Function be called? Why give it this particular scope?
2
3982
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 same name as the parent context, but I want direct access to the parent context's variable. E.g. I would like to be able to do this... function Point()
86
4607
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 method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere."
2
2502
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 seem very offensive to him. For over a year, Crockford has proposed an alternate way of using prototypes like this function object(o) { function F() {} F.prototype = o; return new F();
76
4032
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 crabby old man too; plus he _is_ smart and good).. But, how can he write a book on the good parts of JavaScript and not mention functions that address CSS & DOM? Weird. It's like
8
2661
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: Dojo. Crockford is correct, but I find his logic broken. The prepended underscore says clearly "If we had 'private's this would be one of them." That is useful information, conveyed succinctly.
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8539
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4176
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2759
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 we have to send another system
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.