Its soft because people can still access it outside the class, since
JavaScript doesn't have true private properties yet. Its a good practice in
other languages without real private as well.

But the underscore tells others they can't rely on that property. Its not
part of the class' public API contract.
On Jan 13, 2012 1:22 PM, "piotr_cz" <pkoniec...@hotmail.com> wrote:

> When using Sean's tip, the variable is stored in Class prototype. When
> setting it from class instances method (this._secret = "cheese";)
> makes it public. I guess that's what is called 'soft' ?
>
>
> On Jan 13, 9:52 pm, Olmo Maldonado <olmo.maldon...@gmail.com> wrote:
> > If you want a soft private you should follow Sean's recommendation.
> >
> > As for protected, you can do:
> >
> > new Class({
> >
> >   initialize: function(){
> >
> >   },
> >
> >   method: function(){
> >
> >   }.protected()
> >
> > });
> >
> > If you call klass.method() from outside your class, you'll get an error.
> >
> > As for private, your scheme is fine. You can also:
> >
> > var Klass = (function(){
> >
> >  var privateVariable;
> >
> >  return new Class({
> >     initialize: function(){
> >
> >     },
> >
> >     method: function(){
> >        // privateVariable is only accessible here.
> >     }
> >  });
> >
> > })();
> >
> > On Fri, Jan 13, 2012 at 2:41 PM, Sean McArthur <sean.mons...@gmail.com
> >wrote:
> >
> >
> >
> >
> >
> >
> >
> > > I'd recommend following Python practices on this:
> >
> > > var Example = new Class({
> >
> > >     _secret: 'hello'
> >
> > > });
> >
> > > It's not private like C or Java, but it says "There's no contract with
> > > this property."
>

Reply via email to