Open GoogleCodeExporter opened 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
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
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
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
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
Original issue reported on code.google.com by
bobwayc...@gmail.com
on 5 Feb 2009 at 6:40