Return to Snippet

Revision: 58723
at July 30, 2012 21:28 by timsommer

Initial Code
var Singleton = (function () {
  var instantiated;
  function init() {
    // singleton here
    return {
      publicMethod: function () {
        console.log('hello world');
      publicProperty: 'test'
  return {
    getInstance: function () {
      if (!instantiated) {
        instantiated = init();
      return instantiated;

// calling public methods is then as easy as:

Initial URL

Initial Description
In conventional software engineering, the singleton pattern can be implemented by
creating a class with a method that creates a new instance of the class if one doesn't exist. In the event of an instance already existing, it simply returns a reference to that object.

Initial Title
Javascript Singleton Pattern

Initial Tags

Initial Language