JSFiddle - React, Tailwind, and code Playground
JavaScript
var OtherTest, Singleton, Test;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
Singleton = (function() {
var instance;
function Singleton() {}
instance = null;
Singleton.get = function() {
if (!(this.instance != null)) {
instance = new this;
instance.init();
}
return instance;
};
Singleton.prototype.init = function(name) {
if (name == null) {
name = "unknown";
}
return console.log("" + name + " initialized");
};
return Singleton;
})();
Test = (function() {
__extends(Test, Singleton);
function Test() {
Test.__super__.constructor.apply(this, arguments);
}
Test.prototype.init = function() {
return Test.__super__.init.call(this, "Test");
};
Test.prototype.run = function() {
return alert("OHAI");
};
return Test;
})();
OtherTest = (function() {
__extends(OtherTest, Singleton);
function OtherTest() {
OtherTest.__super__.constructor.apply(this, arguments);
}
OtherTest.prototype.init = function() {
return OtherTest.__super__.init.call(this, "Other Test");
};
OtherTest.prototype.run = function() {
return alert("WUT");
};
return OtherTest;
})();
alert(Test.get() === OtherTest.get()); // should be false
alert(Test.get() === Test.get()); // should be true