Skip to content

Instantly share code, notes, and snippets.

@honbin
Created February 29, 2012 07:18
Show Gist options
  • Save honbin/1938826 to your computer and use it in GitHub Desktop.
Save honbin/1938826 to your computer and use it in GitHub Desktop.
sigletonパターン勉強1
//引用http://addyosmani.com/resources/essentialjsdesignpatterns/book/
var Singleton = (function(){
var instantiated;
function init (){
// singleton here
return {
publicMethod: function(){
console.log( 'hello world' );
},
publicProperty: 'test'
};
}
return {
getInstance: function(){
if ( !instantiated ){
instantiated = init();
}
return instantiated;
}
};
})();
// calling public methods is then as easy as:
Singleton.getInstance().publicMethod();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment