2

I'm writing javascript singleton class and want to use singleton pattern like this:

function TestClass() {
    var self = TestClass.prototype;
    if (self.instance) return self.instance;
    self.instance = this;

    //methods and props declarations
}

var x = new TestClass;
var y = new TestClass;

console.log(x === y); // true

It seems to be worked as i expected, but i worry about memory leaks. So i decided to ask experts, if it is the correct solution

Alex Cube
  • 53
  • 4
  • 1
    You want http://codereview.stackexchange.com/ – Andy Nov 20 '13 at 17:41
  • the `new` keyword creates a new instance, so you are not creating a singleton here – Ibu Nov 20 '13 at 17:41
  • This thread http://stackoverflow.com/questions/1895635/javascript-singleton-question has some good solutions – Rob M. Nov 20 '13 at 17:46
  • Doesn't this JSFiddle suggest that this doesn't work? http://jsfiddle.net/BUqF5/ I was not aware that you could to singletons in JavaScript so this is still new to me – Jason Sperske Nov 20 '13 at 17:48
  • Here's a good SO post about singletons: http://stackoverflow.com/questions/1635800/javascript-best-singleton-pattern – DrCord Nov 20 '13 at 17:55

2 Answers2

1

Not exactly. I generally do something like this when I need a singleton:

function TestClass() {
  if (TestClass.__singleton) {
    return TestClass.__singleton;
  }

  // begin construction ...

  this.a = function() { };
  this.b = 1;

  // ... end construction

  TestClass.__singleton = this;

} // TestClass

var x = new TestClass();  // creates a new TestClass and stores it
var y = new TestClass();  // finds the existing TestClass

console.log(x === y);  // true

y.b = 2;
x.c = 3;

console.log(x.b === y.b && x.c === y.c); // true

If my understanding is correct, subsequent TestClass instantiations, in this case, will create a small, partially-defined TestClass object, but will immediately mark them for garbage collection when TestClass.__singleton is returned.

svidgen
  • 13,744
  • 4
  • 33
  • 58
0

Your code is not wrong per se but not good either. you are not creating a singleton, just "hijack" the prototype. #svidgen gave a correct response.

Peter Aron Zentai
  • 11,482
  • 5
  • 41
  • 71
  • Thanx for your answer. But i'm wondering why "hijacking" the prototype is bad? It works as i expected, but i still worry about memory leaks due to recursive linking. And is the example above (TestClass.__singleton) correct? – Alex Cube Nov 20 '13 at 18:10
  • Yes. That is a more straightforward way than mine. No leaking there, JavaScript has GC. – Peter Aron Zentai Nov 20 '13 at 18:26
  • The hijacked prototype is at minimum weird/uncommon. you dont want anything uncommon when dealing with JavaScript, as it is only the principles that keeps chaos from coming when it comes to large projects in JavaScript. – Peter Aron Zentai Nov 20 '13 at 18:28
  • Also, prototype is not meant to store instance data. except from very specific advanced scenarios prototype should only contain functions, data should be on an instance. – Peter Aron Zentai Nov 20 '13 at 18:32