Skip to content

Instantly share code, notes, and snippets.

@rtfpessoa
Created February 13, 2015 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtfpessoa/b9e036c5ee4bfd455161 to your computer and use it in GitHub Desktop.
Save rtfpessoa/b9e036c5ee4bfd455161 to your computer and use it in GitHub Desktop.
Simple Javascript singleton example
/*
* Simple Javascript singleton example
*/
var Printer = function () {
// Object stateless code here
var counter = 0;
//counter is useless here
this.print("[" + counter++ + "] String to print!");
};
(function () {
// Object state code here
var counter = 0;
this.print = function (str) {
console.log("[" + counter++ + "] Printing...");
console.log(str);
};
}).call(Printer.prototype);
// each instance will keep the previous state
new Printer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment