goupviet / joose-js

Automatically exported from code.google.com/p/joose-js
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Can anything be truly private in modules and/or classes? #11

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I have been playing around with Joose for the past week and have been enjoying 
it. However, I 
have one concern. The documentation on Attributes and building a Class lead me 
to believe that 
Joose handles private attributes and methods. 

Maybe I'm understanding this wrong ... but if I had something like this:

Class("test", {
   has: {
      _name: {
         is: "ro",
         init; ""
      }
   },
   methods: {
      say_name: function() {
         alert(this.getName());
      }
   }
});

how would I access _name via the Joose getter? I know that if it was 'name' the 
getter would be 
called with 'getName()' ... but what about that underscore? get_Name clearly 
won't work ...

Original issue reported on code.google.com by bobwayc...@gmail.com on 5 Feb 2009 at 6:40

GoogleCodeExporter commented 9 years ago
I just realized that my title doesn't really make sense given the content of 
the post. That's because I was going 
to ask a different, though related question.

That second question was essentially how one might go about defining special 
'has' attributes as private so 
some user couldn't open up Firebug, being aware of how things are coded up, and 
issue something like this to 
access private data:

alert(obj._privateAttribute);

Original comment by bobwayc...@gmail.com on 5 Feb 2009 at 6:43

GoogleCodeExporter commented 9 years ago
To the first question. If your instance var is called _name, the name of the 
accessor
is still getName(). The underscore is just a convention to say that outside code
should never read or write the instance var directly. Nothing more.

Practically truly private instance vars are not possible in JS. I wrote about a 
trick
to do it anyway, but the costs are really high:
http://joose-js.blogspot.com/2008/09/truly-private-instance-variables-in.html
It would involve constructing unnique accessor methods for every single object.

You can have privat module level variables. Just create a "var myVar" variable 
inside
the module definition. Code outside the module will not be able to see it.

Original comment by malte.ubl on 5 Feb 2009 at 7:15

GoogleCodeExporter commented 9 years ago
Hmm ... I have some test code that looks like this:

Module("Test", function(m) {
    Class("Account", {
        has: {
            _user: {
                is: "ro",
                init: ""
            },
            _pass: {
                is: "ro",
                init: ""
            }
        },
        methods: {
            authenticate: function () {
                alert("This will authenticate a user with " + this.getUser() + " and " + this.getPass());
        }
         };
});

I try to use it like so:

var t = Test.Account( { _user: "bob", _pass: "secret" } );
t.authenticate();

The alert prints out:

"This will authenticate a user with and "

What have I done wrong?

Original comment by bobwayc...@gmail.com on 5 Feb 2009 at 3:23

GoogleCodeExporter commented 9 years ago
If I remove the underscores, everything works as expected.

But I can still do 

alert(t._user);

Guess creating those via standard var _user is going to have to be the way 
things get done, eh?

Original comment by bobwayc...@gmail.com on 5 Feb 2009 at 3:30

GoogleCodeExporter commented 9 years ago
The initializers use the "public name". The idea is that the underscore are 
never
used outside the class. So try:

var t = Test.Account( { user: "bob", pass: "secret" } );

Original comment by malte.ubl on 5 Feb 2009 at 3:53