Created
February 13, 2015 22:15
-
-
Save rtfpessoa/b9e036c5ee4bfd455161 to your computer and use it in GitHub Desktop.
Simple Javascript singleton example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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