Created
May 23, 2012 12:25
-
-
Save VoQn/2774990 to your computer and use it in GitHub Desktop.
Singleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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