473,770 Members | 6,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Singleton in JavaScript

Hi,

A lot of functions (classes) in my JavaScript app are singletons. So, I have
been exploring ways making JavaScript functions singletons. I thought I'ld
run one idea past you all and get some feed back.

The usual method of making singletons is to have a static member that
returns one instance of the class. But, JavaScript has no notion of static.
The closest it has is prototype functions.

// The singleton class
function Single() {
// call the "static" GetInstance()
return Single.prototyp e.GetInstance() ;
}

// The only instance.
Single.prototyp e._single = null;

// static function
Single.prototyp e.GetInstance = function() {
if( !Single.prototy pe._single ) {
Single.prototyp e._single = this;
}
return Single.prototyp e._single;
}
----------

So, "new Single() == new Single()" is true.
Or "new Single() == Single.prototyp e.GetInstance() "

What do you all think? Is the "new Single()" going to be confusing? Is there
are way to do "Single.GetInst ance()"? I think the prototype version is
unweildy.

-------------------------------------------------------------

When can I refer to prototype functions or prototypes full stop? It appears
that "this.GetInstan ce" exists in "new Single()", but I can't call it.

JohnS
Jul 20 '05 #1
10 8295
"JohnS" <js************ *@optusnet.com. au> wrote in message
news:3f******** *************** @news.optusnet. com.au...
<snip>
... . But, JavaScript has no notion of static.
The closest it has is prototype functions. <snip>

In JavaScript public static members are created a properties of the
function object that represents the constructor:-

function Single() {
if(!Single.inst ance){
//construct objectc
Single.instance = this;
}
return Single.instance ;
}

Single.instance = null; //public static member

- and private static members may be emulated by forming a closure with
the constructor:-

var Single = (function(){
var instance = null; //private static member
return (function(){ //return the constructor function
if(!instance){
//construct object
instance = this;
}
return instance;
});
})(); //inline execution of anonymous function

- (or the prototype, but it is cleaner and more flexible with the
constructor).
... Is there
are way to do "Single.GetInst ance()"?

<snip>

Yes, public static methods are completely normal:-

Single.GetInsta nce = function(){
. . . //function body
}

Richard.
Jul 20 '05 #2

That's great. So, I can drop the prototype and just put GetInstance straight
into the function. I didn't know that.

The second example threw me for a bit. It seemed like it should return a
typeof "function" . But, it returns a typeof "object". So, it's contructing
an object without using the "new" keyword. Strange.

It seems to mean, that when you call a straight function, the function
becomes an object and then executes. If you return "this", you have a
function instance/object.

Also, if you have an inner function, "this" refers to the outer "this".

Thanks for your help.

JohnS

----- Original Message -----
From: "Richard Cornford" <Ri*****@litote s.demon.co.uk>
Newsgroups: comp.lang.javas cript
Sent: Thursday, August 21, 2003 6:01 PM
Subject: Re: Singleton in JavaScript

"JohnS" <js************ *@optusnet.com. au> wrote in message
news:3f******** *************** @news.optusnet. com.au...
<snip>
... . But, JavaScript has no notion of static.
The closest it has is prototype functions.

<snip>

In JavaScript public static members are created a properties of the
function object that represents the constructor:-

function Single() {
if(!Single.inst ance){
//construct objectc
Single.instance = this;
}
return Single.instance ;
}

Single.instance = null; //public static member

- and private static members may be emulated by forming a closure with
the constructor:-

var Single = (function(){
var instance = null; //private static member
return (function(){ //return the constructor function
if(!instance){
//construct object
instance = this;
}
return instance;
});
})(); //inline execution of anonymous function

- (or the prototype, but it is cleaner and more flexible with the
constructor).
... Is there
are way to do "Single.GetInst ance()"?

<snip>

Yes, public static methods are completely normal:-

Single.GetInsta nce = function(){
. . . //function body
}

Richard.

Jul 20 '05 #3
Hi, I just wanted to add a tiny thing that I'm not sure was mentioned.
Another way of declaring a static function is:

function Single.GetInsta nce() {
....
}

This way your call stack will have a named function instead of an
anonymous one.

"Richard Cornford" <Ri*****@litote s.demon.co.uk> wrote in message news:<bi******* ************@ne ws.demon.co.uk> ...

Single.GetInsta nce = function(){
. . . //function body
}

Richard.

Jul 20 '05 #4
b0*****@yahoo.c om (asdf asdf) writes:
Hi, I just wanted to add a tiny thing that I'm not sure was mentioned.
Another way of declaring a static function is:

function Single.GetInsta nce() {
...
}
Not in ECMAScript v3 or any Javascript I have seen. It doesn't appear
to work in JScript.NET (which implements the proposed ECMAScript v4).

It is just a syntax error. It would be very nice is that syntax was
supported, but it isn't.
This way your call stack will have a named function instead of an
anonymous one.


You could write either

function getInstance() {...}
Single.GetInsta nce = getInstance;

or

Single.GetInsta cne = function GetInstance() {...}

Just remember that there is no difference between a "named" function
and an anonymous one. It is just a function object, which is an anonymous
value, that has been assigned to a variable.

/L 'please don't top post'.
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
"JohnS" <js************ *@optusnet.com. au> wrote in message
news:3f******** *************** @news.optusnet. com.au...
<snip>
The second example threw me for a bit. It seemed like it should
return a typeof "function" . But, it returns a typeof "object".
So, it's contructing an object without using the "new" keyword.
Strange.
When you say 'it returns a typeof "object"', what do you men by "it".
Single should be a reference to a function (the constructor returned by
the incline function expression call), as a constructor, calling - new
Single(); - will return an object. A new object the first time it is
called and that same object on every subsequent call. However, if you
call any custom JavaScript constructor without using the - new - keyword
you will not get a new object constructed and there is every chance that
the function will execute as global code and the - this - keyword will
refer to the global object.
It seems to mean, that when you call a straight function, the
function becomes an object and then executes. If you return
"this", you have a function instance/object.
If you call a global function the - this - keyword will refer to the
global object, if - this - is returned then the object returned will be
the global object.
Also, if you have an inner function, "this" refers to the
outer "this".


No it will not. For inner functions - this - could be one of may
objects. If the inner function is assigned as a method of an object
(including function objects) then - this - refers to that object, else
an inner function - this - will be the global object unless that inner
function is called as a constructor using the - new - keyword, in which
case - this - refers to the new object. The specifics are described in
the ECMA Script specification.

Richard.
Jul 20 '05 #6
Single is a reference to a function. But calling Single(), returns an
object. In fact, Single() == new Single(), always.

I now understand why I was confused. When calling Single(), the instance
variable was inited with the global this. Arghh!
So, you are right, you need the new keyword to get a new this. But, after
the first call of new Single(), you can call Single() to get
the instance.

I think I'll change the constructor to create a new Single() if this is
global.

Thanks for your explainations.

----------------------------------------------------------------------------
-------
Here the the test code I used:
note: If you call Single() before new Single(), isObjectGlobal( ) returns
true.
function p( msg ) {
document.write( msg + "<BR>" );
}

var gThis = this;
function isObjectGlobalT his( myThis ) {
return myThis == gThis;
}

// Singleton
var Single = (function(){
var instance = null; //private static member
return (function(){ //return the constructor function
if(!instance){
//construct object
p( "Constructi ng this" );
instance = this;
}
p( "Returning this" );
return instance;
});
})(); //inline execution of anonymous function
function test() {

p( "Calling new Single()" );
single1 = new Single();
p( "" );

p( "Calling Single()" );
single2 = Single();
p( "" );
p( "typeof Single: " + typeof Single );
p( "typeof Single(): " + typeof single1 );
p( "typeof new Single(): " + typeof single2 );
p( "" );

p( "Single() == new Single(): " + (single1 == single2) );
p( "" );

p( "isObjectGlobal This( test ): " + isObjectGlobalT his( this ) );

p( "isObjectGlobal This( Single() ): " + isObjectGlobalT his( single1 ) );
p( "isObjectGlobal This( new Single() ): " + isObjectGlobalT his(
single2 ) );
}

--------------------------------------------------------------------------
Output is:
Calling new Single()
Constructing this
Returning this

Calling Single()
Returning this

typeof Single: function
typeof Single(): object
typeof new Single(): object

Single() == new Single(): true

isObjectGlobalT his( test ): true
isObjectGlobalT his( Single() ): false
isObjectGlobalT his( new Single() ): false
"Richard Cornford" <Ri*****@litote s.demon.co.uk> wrote in message
news:bi******** ***********@new s.demon.co.uk.. .
"JohnS" <js************ *@optusnet.com. au> wrote in message
news:3f******** *************** @news.optusnet. com.au...
<snip>
The second example threw me for a bit. It seemed like it should
return a typeof "function" . But, it returns a typeof "object".
So, it's contructing an object without using the "new" keyword.
Strange.


When you say 'it returns a typeof "object"', what do you men by "it".
Single should be a reference to a function (the constructor returned by
the incline function expression call), as a constructor, calling - new
Single(); - will return an object. A new object the first time it is
called and that same object on every subsequent call. However, if you
call any custom JavaScript constructor without using the - new - keyword
you will not get a new object constructed and there is every chance that
the function will execute as global code and the - this - keyword will
refer to the global object.
It seems to mean, that when you call a straight function, the
function becomes an object and then executes. If you return
"this", you have a function instance/object.


If you call a global function the - this - keyword will refer to the
global object, if - this - is returned then the object returned will be
the global object.
Also, if you have an inner function, "this" refers to the
outer "this".


No it will not. For inner functions - this - could be one of may
objects. If the inner function is assigned as a method of an object
(including function objects) then - this - refers to that object, else
an inner function - this - will be the global object unless that inner
function is called as a constructor using the - new - keyword, in which
case - this - refers to the new object. The specifics are described in
the ECMA Script specification.

Richard.

Jul 20 '05 #7
"JohnS" <js************ *@optusnet.com. au> wrote in message
news:3f******** *************** @news.optusnet. com.au...
Single is a reference to a function. But calling Single(),
returns an object. In fact, Single() == new Single(), always.
As I wrote the Single class it was intended that all calls to it use
the - new - keyword.
I now understand why I was confused. When calling Single(),
the instance variable was inited with the global this. Arghh!
So, you are right, you need the new keyword to get a new this.
But, after the first call of new Single(), you can call
Single() to get the instance.
If you can guarantee that the first call uses - new - and that all calls
that do not use - new - happen after that initial call then you probably
do not want a class at all. Maybe one object assigned to a global
property will do the job sufficiently:-

var Single = {
prop1:1,
prop2:"anyStrin g",
getProp1:functi on(){
return this.prop1;
},
setProp1:functi on(x){
this.prop1 = x;
},
getProp2:functi on(){
return prop2;
}
}

- so all code could just refer to the same object by the identifier
Single.
I think I'll change the constructor to create a new Single()
if this is global.

<snip>

You could do that but you would need a reliable test for the global
object and that would be extra work. If you just want to call a
function - Single() - (without the - new - keyword) and have it always
return the same (non-global) object then there are many aproaches, for
example:-

var Single = (function(){
var instance = null;
function InnerSingle(){
//actual constructor.
this.X = "something" ;
... //constructor function body
}
InnerSingle.pro totype.getX = function(){
return this.X; //example method;
}
return (function(){
if(!instance){
instance = new InnerSingle();
}
return instance;
})
})();

called as - var obj = Single();

- here the real class is the InnerSingle class, whose constructor is
private (inaccessible outside the closure). The rest is just a wrapper
to make acquiring a reference to the one instance convenient. There are
probably at lest another half a dozen ways of achieving the same, using
object literal within the closure, for example:-

var Single = (function(){
var instance = null;
return (function(){
if(!instance){
instance = {
X:"something" ,
getX:function() {
return this.X;
}
};
}
return instance;
})
})();

-or-

var Single = (function(){
var instance = {
X:"something" ,
getX:function() {
return this.X;
}
};
return (funciton(){
return instance;
})
})();

called as - var obj = Single();

Alternatively you could return to your original public static -
getInstance - approach. In JavaScript you cannot have a "class" without
a public constructor, while a singleton probably should not have a
public constructor. However, a similar approach as used above could
emulate a class with a public static getInstance method but no
constructor. In this case the - Single - object is not a function (so it
cannot be called as a constructor), instead it is an object with one
public method:-

var Single = (function(){
var instance = null;
function InnerSingle(){
//actual constructor.
this.X = "something" ;
... //constructor function body
}
InnerSingle.pro totype.getX = function(){
return this.X; //example method;
}
return ({
getInstance:fun ction(){
if(!instance){
instance = new InnerSingle();
}
return instance;
}
});
})();

called as - var obj = Single.getInsta nce();

- the outward appearance and behaviour is that of a class without a
public constructor but with a public static - getInstace - method.

A similar structure could also be used to implement a class with no
public constructor and a factory method that returned a unique new
object on each invocation.

Richard.
Jul 20 '05 #8
Hi -- I believe it works in IE6, which is all I code against. Thanks,
Ron

b0*****@yahoo.c om (asdf asdf) wrote in message news:<6f******* *************** ****@posting.go ogle.com>...
Hi, I just wanted to add a tiny thing that I'm not sure was mentioned.
Another way of declaring a static function is:

function Single.GetInsta nce() {
...
}

This way your call stack will have a named function instead of an
anonymous one.

Jul 20 '05 #9
b0*****@yahoo.c om (asdf asdf) writes:
Hi -- I believe it works in IE6, which is all I code against.


You are actually right.

function x(){}
function x.y (){alert('foo') ;}
x.y(); // alerts "foo"

It is important that x is a function. It doesn't work if it is just a
normal object (which was what I first tried).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2441
by: Phil Powell | last post by:
Consider this: class ActionHandler { /*------------------------------------------------------------------------------------------------------------------------------------------------------------------- This class will be a singleton class structure by calling its constructor by reference only (prefixed by '&'). Is primarly used to reference its errorArray Array property as a single reference to allow for static usage
1
2038
by: Richard A. DeVenezia | last post by:
foo() generates elements with event handlers that invoke foo function properties. Is this an abhorrent or misthought pattern ? It allows just the one occurence of identifier /foo/ to be changed to /whatever/ when need arises and everything should still work. function foo () { var callee = arguments.callee
10
2656
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
3
2498
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL etc. What issues are involved in this, and how would you do this? If someone knows about the...
3
3926
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
2
6347
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any kind of inheritance: singletonObj = new function() { this.prop = true; } Here are two ways to create a singleton with inheritance:
2
1483
by: -Lost | last post by:
Somehow I got lost in an article that led me here: http://www.javascriptkata.com/2007/04/04/how-to-make-a-singleton/ Having recently seen someone else mentioning a Singleton pattern in JavaScript, I wonder, like the commenter Aaron, why anyone would use one? For anyone who doesn't know what a Singleton pattern is, look here: http://en.wikipedia.org/wiki/Singleton_pattern
3
18253
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. Individual users need to be prevented from creating their own instances of the Singleton.
29
1745
by: Ugo | last post by:
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;
0
9592
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10005
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8887
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.