Created
March 6, 2013 21:12
-
-
Save benbabics/5103135 to your computer and use it in GitHub Desktop.
This is in-regard to a comment someone made about having a triple nested return for "getInstance".
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
define([ 'utils/Singleton' ], | |
function(Singleton) { | |
'use strict'; | |
var MyClass; | |
MyClass = (function() { | |
function MyClass() {} | |
MyClass.prototype.methodA = function() {}; | |
MyClass.prototype.methodB = function() {}; | |
MyClass.prototype.methodC = function() {}; | |
return MyClass; | |
})(); | |
return Singleton(MyClass); | |
}); |
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(classInstance) { | |
var __instance = null; | |
return { | |
getInstance: function() { | |
if (__instance === null) { | |
__instance = new classInstance(); | |
} | |
return __instance; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Additionally, if you're not using this within a module, the return for the self-invoking anonymous function would then be
return Singleton(MyClass);
instead of simplyreturn MyClass;
So, it would look something like:
var MyClass;
MyClass = (function() {
...
return Singleton(MyClass);
})();