Skip to content

Instantly share code, notes, and snippets.

@benbabics
Created March 6, 2013 21:12
Show Gist options
  • Save benbabics/5103135 to your computer and use it in GitHub Desktop.
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".
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);
});
function Singleton(classInstance) {
var __instance = null;
return {
getInstance: function() {
if (__instance === null) {
__instance = new classInstance();
}
return __instance;
}
};
}
@benbabics
Copy link
Author

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 simply return MyClass;

So, it would look something like:

var MyClass;
MyClass = (function() {
...
return Singleton(MyClass);
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment