JSFiddle - React, Tailwind, and code Playground
by Alexander
TypeScript
console.clear();
/*
class Singleton {
protected static _instance :Singleton;
protected foo :number = 123;
constructor() {
if (Singleton._instance) {
throw new Error("Instantiation failed: "+
"use Singleton.getInstance() instead of new.");
}
Singleton._instance = this;
}
public static getInstance() :Singleton {
if (Singleton._instance) {
return Singleton._instance;
}
return Singleton._instance = new Singleton;
}
}
//*/
/*
class Singleton {
protected static _instance :Singleton = new Singleton;
protected foo :number = 123;
constructor() {
if (Singleton._instance) {
throw new Error("Instantiation failed: "+
"use Singleton.getInstance() instead of new.");
}
}
public static getInstance() :Singleton {
return Singleton._instance;
}
}
//*/
/*
namespace Singleton {
const instance = {
foo: 123
};
let getInstance = () :instance => instance;
export getInstance;
}
//*/
/*
module Singleton {
interface Instance {
foo: number;
}
var instance :Instance = {
foo: 123
};
export function getInstance() :Instance {
return instance;
}
}
//*/
/*
const Singleton = {
foo: 123,
getInstance() { return this }
};
//*/
/*
function Singleton() {
if (arguments.callee.instance) return arguments.callee.instance;
return arguments.callee.instance = this;
}
/*
(function(global){
/** @type {SingletonConstructor} *
var instance;
global.Singleton = function() {
if (instance !== void 0) return instance;
return instance = this;
};
global.Singleton.prototype.foo = 123;
})(window);
//*/
/*
function Singleton() {
if (Singleton.instance) return Singleton.instance;
this.foo = 123;
return Singleton.instance = this;
}
//*/
/*
const Singleton = new (class {
public foo :number = 123;
getInstance() :this { return this }
})();
//*/
let singleton = Symbol();
let singletonEnforcer = Symbol()
class Singleton {
constructor(enforcer) {
if (enforcer !=...