Thank you for a full and detailed explanation!

On 7 мар, 11:09, "T.J. Crowder" <[email protected]> wrote:
> > I need to acces to private variables in my class like in C#
> > How can I realize it in Prototype?
>
> Prototype doesn't give you anything here, so you're just looking at
> how to do it with JavaScript.
>
> JavaScript doesn't have private variables. The equivalent of private
> *class* variables (e.g., statics in C#) is very easy to emulate using
> the module pattern. Private *instance* variables, as in your C#
> example code, you can emulate using closures[1], but at the expense
> that any function that needs access to the private variable will be
> *duplicated* for every instance of your object.
>
> Example:
>
> var Thingy = Class.create({
>
>   initialize: function() {
>     var trulyPrivate = 0;
>
>     this.getNextPrivateValue = function() {
>       return ++trulyPrivate;
>     };
>
>   },
>
>   doSomething: function() {
>     // This does *not* have access to `trulyPrivate`
>   }
>
> });
>
> (http://jsbin.com/oleko6/2)
>
> Every instance of the `Thingy` "class" has a truly private variable,
> the `trulyPrivate` local variable within `initialize`. `Thingy` has
> only one function that can access it, the `getNextPrivateValue`
> function. (Well, okay, two; `initialize` can access it as well, of
> course!) The `getNextPrivateValue` function is created within
> `initialize` and so it closes over that local variable, which can't be
> seen from outside. Because the closure endures after the return from
> `initialize`, so does the variable, because it exists on something
> called the "variable object" for the call to `initialize`, which the
> closure has an enduring reference to. That means it can read and write
> that variable, even though it's not a property of the instance.
>
> Note that `doSomething` doesn't have access to `trulyPrivate`, because
> the only things it has access to within the instance would be through
> its `this` reference, which has only the properties that anything else
> (outside `Thingy`) can see. This is a consequence of the fact that
> JavaScript doesn't have methods, it just has functions.[2]
>
> The cost is that the `getNextPrivateValue` function is created for
> *every* instance of `Thingy`. So if you create 20 `Thingy` instances,
> you'll have 20 copies of the `getNextPrivateValue` function in memory.
> That's fine if you're only going to have a few of these things in
> memory at any given time; if you're going to have hundreds or
> thousands of them, that's probably not fine and you should just use a
> property that you tell people not to touch.
>
> [1]http://blog.niftysnippets.org/2008/02/closures-are-not-complicated.html
> [2]http://blog.niftysnippets.org/2008/03/mythical-methods.html
>
> HTH,
> --
> T.J. Crowder
> Independent Software Engineer
> tj / crowder software / com
> www / crowder software / com
>
> On Mar 7, 7:05 am, buda <[email protected]> wrote:
>
>
>
>
>
>
>
> > I need to acces to private variables in my class like in C#
>
> > ...
> >   protected bool _visible;
> >   public bool Visible
> >   {
> >     get
> >     {
> >       return _visible;
> >     }
> >     set
> >     {
> >       _visible = value;
> >     }
> >   }
>
> > How can I realize it in Prototype?
> > Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to