Last active
December 18, 2015 00:09
-
-
Save bloodyowl/5695070 to your computer and use it in GitHub Desktop.
singleton pattern
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 mySingleton = (function(){ | |
var instance = null | |
function K(){ | |
if(!instance || this !== instance) throw "Constructor is a singleton" | |
return this | |
} | |
K.prototype.foo = function(){} | |
K.prototype.bar = function(){} | |
K.prototype.bar = function(){} | |
return function(){ | |
if(!instance) instance = K.call(Object.create(Constructor.prototype)) | |
return instance | |
} | |
})() |
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
function singleton(object){ | |
var instance = null | |
return function(){ | |
if(!instance) instance = Object.create(object) | |
return instance | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@piecioshka True, that's because I decided to use
Object.create
afterwards. I renamed it to a genericK
.