I have Singleton
implementation like this
var Singleton = (function () {
var instance;
function Singleton() {
if (!instance) {
instance = this;
}
else {
return instance;
}
}
return Singleton;
}());
And it's work if you create new object with new
keyword, like
console.log(new Singleton() === new Singleton());
But if someone forgets to use new
keyword, it doesn't work
console.log(Singleton() === Singleton());
The question is how to force function to be called as constructor-function, even without new
keyword, to make these work:
console.log(Singleton() === Singleton());
console.log(new Singleton() === new Singleton());
console.log(new Singleton() === Singleton());