Instantiate a javascript module only one time.
Posted
by Cedric Dugas
on Stack Overflow
See other posts from Stack Overflow
or by Cedric Dugas
Published on 2010-05-29T23:42:47Z
Indexed on
2010/05/29
23:52 UTC
Read the original article
Hit count: 173
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?
© Stack Overflow or respective owner