Skip to content

Instantly share code, notes, and snippets.

@jasonwyatt
Created July 26, 2011 15:09

Revisions

  1. jasonwyatt created this gist Jul 26, 2011.
    30 changes: 30 additions & 0 deletions MySingleton.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    define(function(){
    var instance = null;

    function MySingleton(){
    if(instance !== null){
    throw new Error("Cannot instantiate more than one MySingleton, use MySingleton.getInstance()");
    }

    this.initialize();
    }
    MySingleton.prototype = {
    initialize: function(){
    // summary:
    // Initializes the singleton.

    this.foo = 0;
    this.bar = 1;
    }
    };
    MySingleton.getInstance = function(){
    // summary:
    // Gets an instance of the singleton. It is better to use
    if(instance === null){
    instance = new MySingleton();
    }
    return instance;
    };

    return MySingleton.getInstance();
    });