Singleton pattern in RequireJS
Yes this is possible and has its advantage. I use it often for Toolbox-like functions.
This is the basic approach
define(function (require) {
var singleton = function ()
{
return {
...
};
};
return singleton();
});
Other approaches that are less ideal but work anyhow
This method uses the "new". It is good, but not needed as you can see in my code above.:
https://gist.github.com/jasonwyatt/1106973
Some credit for this post goes to ggozad from stackoverflow :
http://stackoverflow.com/questions/9733201/is-it-a-bad-practice-to-use-the-requirejs-module-as-a-singleton
Recent Comments