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',
'}'
);
}
}


|