Created
February 29, 2012 07:18
-
-
Save honbin/1938826 to your computer and use it in GitHub Desktop.
sigletonパターン勉強1
This file contains hidden or 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
//引用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