Hi Mona,

> Are the private methods/functions being
> duplicated for each of myclass instance?

No, there is a single copy of each private variable and private
method, contained within the outer closure, which is referenced by
public methods you define because they, too, are closures.  That means
all instances of the class will share the same "private" variables and
functions.  Consequently, given this:

* * * *
Object.extend(myclass.prototype, function() {
        //private variables
        var name;

        //private methods
        function doCap () { name = name.toUpperCase(); }

        return {
                //public methods
                getName: function()  { return name; },
                setName: function(n) { name = n;    },
                capName: function(n) { doCap(); }
        };
}());
* * * *

This code:

* * * *
    var a, b;

    a = new myclass();
    b = new myclass();

    a.setName('a');
    alert('b.getName(): ' + b.getName());
    b.capName();
    alert('a.getName(): ' + a.getName());
* * * *

...alerts "b.getName(): a" then "a.getName(): A" because both
instances are sharing the same instance of the 'name' private member.
Which is fine, if it's what you're trying to do.

The thing to remember here is that 'name' (in my example) is *not* a
property of myclass, myclass.prototype, or an instance of myclass --
it's a variable contained in a closure.  (Technically, it's a property
of the closure's variable object.[1]).

[1] http://www.ecma-international.org/publications/standards/Ecma-262.htm

If you're trying to get private instance, there are various ways to do
that, all of which come with a cost of some kind.

HTH,
--
T.J. Crowder
tj / crowder software / com

On Nov 17, 9:13 am, "Mona Remlawi" <[EMAIL PROTECTED]> wrote:
> Dear Prototypers,
> I've been using the following technique for separating private and
> public methods/variables in a class declaration.
>
> ---
> Object.extend(myclass.prototype, function() {
>         //private variables
>         var attr1;
>         var attr2;
>         ...
>         //private methods
>         function meth1 () {}
>         function meth2 () {}
>         ...
>
>         return {
>                 //public methods
>                 pub_meth1: function() {},
>                 pub_meth2: function() {}
>         }}());
>
> ---
>
> I'm wondering if this is a good practice. Are there any insights that
> i should be aware of? Are the private methods/functions being
> duplicated for each of myclass instance?
>
> Thanks in advance
>
> --
> mona
> [EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
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 prototype-scriptaculous@googlegroups.com
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