Last active
August 29, 2015 14:18
-
-
Save Williammer/9af8b88896ab7b83333d to your computer and use it in GitHub Desktop.
implement a singleton klass with redefine constructor pattern and IIFE.
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
// Constructor way | |
function SingletonKlass() { | |
var instance; | |
SingletonKlass = function SingletonKlass() { | |
return instance; | |
} | |
SingletonKlass.prototype = this; | |
instance = new SingletonKlass(); | |
instance.constructor = SingletonKlass; | |
// properties. | |
instance.createdTime = 0; | |
instance.getRandNum = Math.random(); | |
return instance; | |
} | |
// IIFE way | |
var SingletonKlass; | |
(function() { | |
var instance; | |
SingletonKlass = function SingletonKlass() { | |
if(instance){ | |
return instance; | |
} | |
instance = this; | |
// properties. | |
instance.createdTime = 0; | |
instance.getRandNum = Math.random(); | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment