Singleton without a closure

Warning: anti-pattern ahead.

Mwahaha, I feel like a ninja. I mean a ninja by Doug Crockford's definition which means someone who finds a mistake in the language's design, decides it's cool and abuses it.

So there you go. Regular expression objects are created only once if you're using a literal. Such an object can be used to store the single instance of a Singleton() constructor.


function Single() {
    "strict me not!";
    var re = / /;
    if (re.instance) {
        return re.instance;
    re.instance = this; = "Foo";
Single.prototype.getName = function () {


var s1 = new Single(),
    s2 = new Single();
console.log(s1 === s2); // true
console.log(s1.getName()); // "Foo" = "dude";
console.log(s2.getName()); // "dude"


  • The prototype chain works fine
  • No closures
  • No public properties or globals to store the single instance


  • It's a hack
  • ES5 defines that reg exp literals should no longer work like this


Don't use. Please 🙂


Sorry, comments disabled and hidden due to excessive spam. Working on restoring the existing comments...

Meanwhile you can find me on twitter - @stoyanstefanov