Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > JavaScript > Re: Singleton
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 19 of 30 Topic 35510 of 37166
Post > Topic >>

Re: Singleton

by VK <schools_ring@[EMAIL PROTECTED] > May 24, 2008 at 01:31 PM

On May 24, 8:13 pm, VK <schools_r...@[EMAIL PROTECTED]
> wrote:
> On May 24, 7:58 pm, Ugo <priv...@[EMAIL PROTECTED]
> wrote:
>
>
>
> > >>> The best way of doing something depends at least in part on what
it is
> > >>> you are trying to do.
> > >> A generic way to create a singleton object
> > > I doubt that is what Richard meant by "what it is you are trying to
> > > do". You are more likely trying to "implement a tabbed pane widget"
or
> > > "send user data to the server". Unless this is just an academic
> > > exercise.
>
> > all this things  :P
>
> > > A generic way to create a singleton object with global access is to
> > > use a object literal.
>
> > > var someSingleton = {
> > >   someProperty: function() {},
> > >   someOtherProperty: 55
> > > };
>
> > But, in this way I can not to declare "private" variables/methods in
an
> > elegant way:
>
> > I don't like it:
>
> > var singleton;
>
> > (function()
> > {
> >         // "privare" prop
> >         var a = 1;
> >         // "private" method
> >         function b( )
> >         {
> >                 alert('hi');
> >         }
>
> >         // Obj
> >         singleton = {
> >                 hi : b,
> >                 a: a
> >         };
>
> > })();
>
> The way I'm using most often - without any claims that it is the best
> out of anything:
>
> function MySingleton() {
>  if (this == self) {
>   window.alert('Conctructor called as a function');
>   return MySingleton;
>  }
>  else if (
>  !!($isInstantiated.flag) ||
>  (this instanceof MySingleton) ||
>  (MySingleton.isPrototypeOf(this))
>  ) {
>   window.alert('Only one instance is allowed');
>   return mySingleton;
>  }
>  else {
>   // instantiate your singleton
>   // with methods and properties
>   // ...
>   this.toString = $toString;
>   $isInstantiated.flag = true;
>  }
>  function $isInstantiated() {
>   return arguments.callee.flag;
>  }
>  function $toString() {
>   return ''.concat(
>    'function mySingleton() {\n',
>    '    [native code]\n',
>    '}'
>   );
>  }
>
> }
>
> The benefits as I see them:
> 1) It is much lesser convoluted as function expression ways.
> Respectively it is easier to read, to maintain and to avoid parasite
> closures.
> 2) It allows to distinct different "not allowed" situation so to
> provide more informative errors - in a real case window.alert replaced
> by throw new Error of course.
> 3) it prevents or at least makes much harder to use reliably runtime
> cloning over source code dumping and reusing in eval or new Function.

A few errors made while pulling out the code and readjusting for
posting, sorry. The corrected version would be:

function MySingleton() {
 if (this == self) {
  window.alert('Conctructor called as a function');
  return MySingleton;
 }
 else if (
 !!($isInstantiated.flag) &&
 ((this instanceof MySingleton) ||
 (MySingleton.isPrototypeOf(this)))
 ) {
  window.alert('Only one instance is allowed');
  return MySingleton;
 }
 else {
  // instantiate your singleton
  // with methods and properties
  // ...
  this.toString = $toString;
  $isInstantiated.flag = true;
 }
 function $isInstantiated() {
  return arguments.callee.flag;
 }
 function $toString() {
  return ''.concat(
   'function mySingleton() {\n',
   '    [native code]\n',
   '}'
  );
 }
}
 




 30 Posts in Topic:
Singleton
Ugo <privacy@[EMAIL PR  2008-05-07 10:42:51 
Re: Singleton
Sam -- <noemail@[EMAIL  2008-05-07 14:39:24 
Re: Singleton
Ugo <privacy@[EMAIL PR  2008-05-07 18:14:25 
Re: Singleton
RoLo <rolosworld@[EMAI  2008-05-07 08:06:37 
Re: Singleton
RoLo <rolosworld@[EMAI  2008-05-07 08:08:10 
Re: Singleton
"Richard Cornford&qu  2008-05-21 22:20:21 
Re: Singleton
Ugo <privacy@[EMAIL PR  2008-05-23 16:22:42 
Re: Singleton
Thomas 'PointedEars' Lahn  2008-05-23 20:45:26 
Re: Singleton
"Richard Cornford&qu  2008-05-26 16:19:01 
Re: Singleton
Peter Michaux <petermi  2008-05-23 11:07:58 
Re: Singleton
Ugo <privacy@[EMAIL PR  2008-05-24 17:58:50 
Re: Singleton
"Richard Cornford&qu  2008-05-26 16:18:58 
Re: Singleton
"P. Prikryl" &l  2008-05-23 12:30:40 
Re: Singleton
Thomas 'PointedEars' Lahn  2008-05-23 21:54:35 
Re: Singleton
Peter Michaux <petermi  2008-05-23 12:43:47 
Re: Singleton
VK <schools_ring@[EMAI  2008-05-24 09:13:44 
Re: Singleton
Peter Michaux <petermi  2008-05-24 09:25:50 
Re: Singleton
Ugo <privacy@[EMAIL PR  2008-05-25 00:02:02 
Re: Singleton
VK <schools_ring@[EMAI  2008-05-24 13:31:52 
Re: Singleton
Peter Michaux <petermi  2008-05-24 16:19:51 
Re: Singleton
Ugo <privacy@[EMAIL PR  2008-05-25 09:01:38 
Re: Singleton
"Richard Cornford&qu  2008-05-26 16:19:05 
Re: Singleton
VK <schools_ring@[EMAI  2008-05-25 01:31:38 
Re: Singleton
Lasse Reichstein Nielsen   2008-05-25 13:42:07 
Re: Singleton
Lasse Reichstein Nielsen   2008-05-25 13:52:35 
Re: Singleton
VK <schools_ring@[EMAI  2008-05-25 05:07:35 
Re: Singleton
Lasse Reichstein Nielsen   2008-05-25 15:11:19 
Re: Singleton
VK <schools_ring@[EMAI  2008-05-25 08:35:40 
Re: Singleton
Peter Michaux <petermi  2008-05-25 10:18:55 
Re: Singleton
Peter Michaux <petermi  2008-05-21 21:14:21 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 9:46:28 CDT 2008.