HI,

In the pattern you are using, `privateMember` is a static or class
property.

It is _shared_ between all instances of the same class, hence what you
observed above.

If you want true privacy, you'll need the following:

var MySuperclass = Class.create({
  initialize : function(value) {
    this.bar = function() {
      alert(value);
    }
  }
});

Note that this is closure-based and comes at the expense of needing a
`bar` function for each instance of MySuperclass (rather than using a
single one one MySuperclass.prototype).

Hope this helps.

Best,

Tobie


On Jul 3, 10:13 pm, Matías M. <[email protected]> wrote:
> Hello guys. I guess to ask you something about the inheritance in
> Prototype. I'm implementing a private-member pattern in my classes,
> and it seems that's not working when using the inheritance in
> Prototype 1.6.
>
> Please, look at the example below:
>
> var MySuperclass = Class.create(function() {
>
>   var privateMember = "foo";
>
>   var publicInterface = {
>     initialize : function(theValue) {
>       privateMember = theValue;
>     },
>     bar : function() {
>       alert(foo);
>     }
>   };
>
>   return publicInterface;
>
> }());
>
> In this approach the privateMember attribute is trapped into the
> function scope and is not visible for the public object, but it's
> visible inside the object. When I try to extend this class from
> another one, the "private" attribute is replaced every time a new
> instance is created. For example:
>
> var MySubclass1 = Class.create(MySuperclass, function() {
>   var publicInterface = {
>     initialize : function($super, theValue) {
>       $super(theValue);
>     }
>   };
>
>   return publicInterface;
>
> }());
>
> var MySubclass2 = Class.create(MySuperclass, function() {
>   var publicInterface = {
>     initialize : function($super, theValue) {
>       $super(theValue);
>     }
>   };
>
>   return publicInterface;
>
> }());
>
> var obj1 = new MySubclass1("Hello");
> var obj2 = new MySubclass2("World");
>
> obj1.bar();
> obj2.bar();
>
> In any case, it'll display "World" instead "Hello", and the right way
> should be to display "Hello" in the first case. I found a way to fix
> this behavior but it's not applied to Prototype 1.6. The fixing
> consists of giving an instance of the class instead the class
> reference to the Class.create method. See below:
>
> var MySubclass2 = Class.create(new MySuperclass(), function() {
>   var publicInterface = {
>     initialize : function($super, theValue) {
>       $super(theValue);
>     }
>   };
>
>   return publicInterface;
>
> }());
>
> Please, I need to know if there's any way in order to implement my
> approach, since I have a lot of work done in this way.
>
> Thank you beforehand,
>
> Matías M.
--~--~---------~--~----~------------~-------~--~----~
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