Skip to content

Instantly share code, notes, and snippets.

@VoQn
Created May 23, 2012 12:25
Show Gist options
  • Save VoQn/2774990 to your computer and use it in GitHub Desktop.
Save VoQn/2774990 to your computer and use it in GitHub Desktop.
Singleton
var Singleton = (function(){
var Singleton = function(){}
, privates
, method
, name
, self = new Singleton();
privates = { // ここはプラベートスコープにできる
isSingleton: true
, name: 'singleton'
};
method = { // ここでパブリックメソッド(というかクラスメソッド)のスタブを書く
getName: function(){
return privates.name;
},
setName: function( name ){
privates.name = name;
},
isSingleton: function(){
return privates.isSingleton;
}
};
for ( name in method ){
Singleton.prototype[ name ] = method[ name ]; // クラスメソッド化
}
return self;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment