/ Published in: JavaScript
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// WAY 1 - Instantiating an anonymous function var MySingleton = new (new MyClass(args)); // WAY 2 - Using an instance manager var getMySingleton = (function(options) { function MyClass(args) {} var singleton; return function(args) { return singleton ? singleton : singleton = new MyClass(args); } })(); // WAY 3 - The simple way var MySingleton = { prop: "foo", method: function() {}, ... };