HighDots Forums  

Singleton

Javascript JavaScript language (comp.lang.javascript)


Discuss Singleton in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ugo
 
Posts: n/a

Default Singleton - 05-07-2008 , 04:42 AM






Hi guys,

how do you make a singleton access class?

Do you know a better way of this one:

var singletonClass = (function( )
{
// Private variable
var instance = null;

// Private Constructor
function myClass( )
{
//...
}

return new function( )
{
this.constructor = null;

this.getInstance = function( )
{
if( ! instance )
{
instance = new myClass( );
instance.constructor = null;
}

return instance;
}
}
})( );

Reply With Quote
  #2  
Old   
Sam --
 
Posts: n/a

Default Re: Singleton - 05-07-2008 , 08:39 AM






On Wed, 7 May 2008 10:42:51 +0200, Ugo <privacy (AT) nospam (DOT) it> wrote:

Quote:
Hi guys,

how do you make a singleton access class?

Do you know a better way of this one:

var singletonClass = (function( )
{
// Private variable
var instance = null;

// Private Constructor
function myClass( )
{
//...
}

return new function( )
{
this.constructor = null;

this.getInstance = function( )
{
if( ! instance )
{
instance = new myClass( );
instance.constructor = null;
}

return instance;
}
}
})( );
Pure qui? :-))
se ti vede ZERO...


ciao


Reply With Quote
  #3  
Old   
RoLo
 
Posts: n/a

Default Re: Singleton - 05-07-2008 , 11:06 AM



Quote:
Do you know a better way of this one:
Yes

change this:
* *return new function( )

to this:
return function( )

;-)


Reply With Quote
  #4  
Old   
RoLo
 
Posts: n/a

Default Re: Singleton - 05-07-2008 , 11:08 AM



On May 7, 11:06*am, RoLo <roloswo... (AT) gmail (DOT) com> wrote:
Quote:
Do you know a better way of this one:

Yes

change this:
** *return new function( )

to this:
* * return function( )

;-)
ok... sorry, I over read you code.. you can ignore my previous comment.


Reply With Quote
  #5  
Old   
Ugo
 
Posts: n/a

Default Re: Singleton - 05-07-2008 , 12:14 PM



Quote:
Hi guys,
how do you make a singleton access class?
Do you know a better way of this one:
[cut]
Pure qui? :-))
ehh, giā
Mi č tornato questo cruccio, e non so' fare nč trovare di meglio...
per tanto o provato a vedere cosa ne pensano qui...

Quote:
se ti vede ZERO...
ssshhh, zitto, non dire niente che forse non se ne accorge
:P

Quote:
ciao
Bye


Reply With Quote
  #6  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Singleton - 05-21-2008 , 05:20 PM



Ugo wrote:
Quote:
how do you make a singleton access class?
How do you define a "singleton access class"?

Quote:
Do you know a better way of this one:
The best way of doing something depends at least in part on what it is
you are trying to do.

Quote:
var singletonClass = (function( )
{
// Private variable
var instance = null;
There is little point in assigning null to this variable as its default
undefined value will be just as false as the null value when you test it
with - !instance -.

Quote:
// Private Constructor
function myClass( )
{
//...
}

return new function( )
I can think of no circumstances under which it makes sense to use the -
new - operator with a function expression as its operand. In this case
it would be simpler (and more efficient) to have an object literal
returned at this point. That object could easily define - constructor -
and - getInstance - properties.

Quote:
{
this.constructor = null;

this.getInstance = function( )
{
if( ! instance )
{
instance = new myClass( );
instance.constructor = null;
As you are using a constructor to create this object instance it would
be possible to define the created object's - constructor property on the
prototype of the constructor. Though there may be no good reason for
using a contractor to create the object at all as you are only going to
be creating one instance of the object, and so again a literal could be
used instead.

Quote:
}

return instance;
}
}
})( );
The result of those changes could look like:-

var singletonClass = (function( ){
// Private variable
var instance;
return ({
constructor:null,
getInstance:function( ){
return (
instance||
(
instance = {
constructor:null
}
)
);
}
});
})();

- though that is almost certainly less than would be needed in any real
context, but you will always suffer that if you don't define what it is
exactly that you are trying to achieve.

Richard.



Reply With Quote
  #7  
Old   
Peter Michaux
 
Posts: n/a

Default Re: Singleton - 05-22-2008 , 12:14 AM



On May 7, 1:42 am, Ugo <priv... (AT) nospam (DOT) it> wrote:
Quote:
Hi guys,

how do you make a singleton access class?

Do you know a better way of this one:

var singletonClass = (function( )
{
// Private variable
var instance = null;

// Private Constructor
function myClass( )
{
//...
}

return new function( )
{
this.constructor = null;

this.getInstance = function( )
{
if( ! instance )
{
instance = new myClass( );
instance.constructor = null;
}

return instance;
}
}

})( );
This does not look like something a JavaScript programmer would write.
In JavaScript, assigning an object literal to a global variable would
likely achieve the purpose of a Singleton pattern in Java, for
example, for many situations. What is your real application?

Peter


Reply With Quote
  #8  
Old   
Ugo
 
Posts: n/a

Default Re: Singleton - 05-23-2008 , 10:22 AM



Quote:
how do you make a singleton access class?
How do you define a "singleton access class"?
i wanted a function/object which restricts the instantiation of one my
object one time and which it ables one global access

Quote:
Do you know a better way of this one:

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 (and if it's possible I'd like
to adopt the GoF's "roles" - applyed to JS)

Quote:
var singletonClass = (function( )
{
// Private variable
var instance = null;

There is little point in assigning null to this variable as its default
undefined value will be just as false as the null value when you test it
with - !instance -.
IMHO, (new myClass()) can not be null|false|undefined..
so that initialization was good

Quote:
// Private Constructor
function myClass( )
{
//...
}

return new function( )

I can think of no circumstances under which it makes sense to use the -
new - operator with a function expression as its operand. In this case
it would be simpler (and more efficient) to have an object literal
returned at this point. That object could easily define - constructor -
and - getInstance - properties.
ahh, in effect :P

Quote:
{
this.constructor = null;

this.getInstance = function( )
{
if( ! instance )
{
instance = new myClass( );
instance.constructor = null;

As you are using a constructor to create this object instance it would
be possible to define the created object's - constructor property on the
prototype of the constructor. Though there may be no good reason for
using a contractor to create the object at all as you are only going to
be creating one instance of the object, and so again a literal could be
used instead.
Mmm

Quote:
}

return instance;
}
}
})( );

The result of those changes could look like:-
let's look at

Quote:
var singletonClass = (function( ){
// Private variable
var instance;
return ({
constructor:null,
getInstance:function( ){
return (
instance||
(
instance = {
constructor:null
}
)
);
}
});
})();
Ok, it's no much different of mine, you have converted my code with object
literal

Quote:
- though that is almost certainly less than would be needed in any real
context, but you will always suffer that if you don't define what it is
exactly that you are trying to achieve.
see before

THKS


Reply With Quote
  #9  
Old   
Peter Michaux
 
Posts: n/a

Default Re: Singleton - 05-23-2008 , 02:07 PM



On May 23, 7:22 am, Ugo <priv... (AT) nospam (DOT) it> wrote:
Quote:
how do you make a singleton access class?
How do you define a "singleton access class"?

i wanted a function/object which restricts the instantiation of one my
object one time and which it ables one global access

Do you know a better way of this one:

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.

A generic way to create a singleton object with global access is to
use a object literal.

var someSingleton = {
someProperty: function() {},
someOtherProperty: 55
};

Peter


Reply With Quote
  #10  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Singleton - 05-23-2008 , 02:45 PM



Ugo wrote:
Quote:
IMHO, (new myClass()) can not be null|false|undefined..
Yes, it can ;-)

function myClass()
{
this.toString = function() {
var a = [null, false, a];
return a[Math.floor(Math.random() * a.length)];
};
}

window.alert(new myClass());


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.