//http://www.jermywells.com //js singleton "use strict"; var Singleton = function() { var instanceName = ""; if (Singleton.prototype.instance) { return Singleton.prototype.instance; } Singleton.prototype.instance = this; this.setInstanceName = function(n){ instanceName = n; }; this.getInstanceName = function(){ return instanceName; }; }; var firstSingleton = new Singleton(); firstSingleton.setInstanceName('first One'); var secondSingleton = new Singleton(); secondSingleton.setInstanceName('second One'); document.getElementById('first').innerHTML = firstSingleton.getInstanceName(); document.getElementById('second').innerHTML = secondSingleton.getInstanceName();
<div id="first"></div> <div id="second"></div>