Re:Loyal Opposition to Const, Private, Freeze, Non-Configura...
by davedx on Wednesday November 02, 2011 @07:09PM
(#3189342)
Being able to declare member variables as private (really private, not the private alluded to in Ruby) is good. As the OP said, you don't have to declare a member variable as private if you want relaxed 0constraints. But when you want to for good design reasons -- it's great that the language you are working with allows you to do so.
[ Parent
| on HN ]
Re:Loyal Opposition to Const, Private, Freeze, Non-Configura...
by repsilat on Wednesday November 02, 2011 @08:06PM
(#3189518)
If you really need private member variables you can probably whip something up with lambdas, though it might be a little awkward.
Re:Loyal Opposition to Const, Private, Freeze, Non-Configura...
by reissbaker on Wednesday November 02, 2011 @08:30PM
(#3189571)
It's super easy:
function A {
var secret = 0; // <---------- private variable
this.method = function() {
alert(secret);
};
this.increment = function() {
secret++;
};
}
a = new A();
a.increment();
a.method(); <-------- prints 1
The main problem is just the inefficiency of declaring methods inside constructors.Edit: Hence, the keyword.