Singleton Pattern or Rubbish?

L

-Lost

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

"the singleton pattern is a design pattern that is used to restrict instantiation of a
class to one object"

Which basically means we end up with an object that has the ability to restrict itself to
only one instance at a time.

Anyway, I would appreciate any *clear* examples of how one might *require* a Singleton
pattern.

-Lost
 
D

Douglas Crockford

-Lost said:
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?

JavaScript can support much simpler formulations. For example:

var catNames = function () {
var names = [];
return {
add: function (catName) {
names.push(catName);
},
removeLast: function () {
return names.pop();
},
alertAllCats: function () {
alert(names.join(","));
}
};
}();

http://javascript.crockford.com/
 
S

scripts.contact

-Lost said:
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? ..........................
Anyway, I would appreciate any *clear* examples of how one might *require* a Singleton
pattern.

http://prototyp.ical.ly/index.php/2007/03/01/javascript-design-patterns-1-the-singleton/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top