Julian Turner wrote:
>
> Julian Turner wrote:
>
> [snip]
>> i could expose both to the public, but is there a way to keep them
>> > private AND use them. any patterns ?
> [snip]
>
> Here is a quick example:-
>
> var myObject=(function(){
>
> var myPrivateVar=1;
> var myPrivateFunction=function(){
> alert("Hello from Private "+myPrivateVar);
> }
>
> function Constructor(){}
>
> Constructor.prototype.alert=function()
> {
> myPrivateFunction();
> myPrivateVar++;
> }
>
> return Constructor;
> })();
>
>
> var o1=new myObject();
> var o2=new myObject();
> o1.alert();
> o2.alert();
>
>
Except that in Gerald's original post 'privateVar' was a private instance
variable and you've demonstrated how to make a private variable shared
between all instances which is not the same thing at all.
Received on Tue Feb 7 21:29:41 2006