Instantiate a javascript module only one time.
- by Cedric Dugas
Hey guys,
I follow a module pattern where I instantiate components, however, a lot of time a component will only be instantiate one time (example: a comment system for an article).
For now I instantiate in the same JS file. but I was wondering if it is the wrong approach? It kind of make no sense to instantiate in the same file and always only once. But at the same time, if this file is in the page I want to have access to my module without instantiate from elsewhere, and IF I need another instance, I just create another from elsewhere...
Here is the pattern I follow:
ApplicationNamespace.Classname = function() {
// constructor
function privateFunctionInit() {
// private
}
this.privilegedFunction = function() {
// privileged
privateFunction();
};
privateFunctionInit()
};
ApplicationNamespace.Classname.prototype = {
Method: function(){}
}
var class = new ApplicationNamespace.Classname();
What do you think, wrong approach, or is this good?