LoginSignup
8
8

More than 5 years have passed since last update.

JSでSingleton

Last updated at Posted at 2014-04-08

毎度忘れるのでメモ。
コマいのはあるけど大抵事足りる。

var Foo = function(){
  //Singleton
  if (!Foo.instance) {
    Foo.instance = this;
    this.init.apply(this, arguments);
  }
  return Foo.instance;
};
Foo.getInstance = function(){
  return new Foo();
};
Foo.prototype.init = function(){
  console.log("Foo init");
};
Foo.getInstance();
new Foo();
Foo();
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8