I’m trying to learn the JavaScript module pattern referenced here. Everything looks great but I keep seeing different ways to create the module.
In the article referenced above he access the module’s public methods by using YAHOO.myProject.myModule.init(); without calling new. But then I’ve seen a few people do something like var module = new YAHOO.myProject.myModule();. Should new be used and why?
There is no right or wrong answer to this.
Sometimes a module will contain an object, where no new keyword is needed.
Sometimes a module will contain a constructor for an object, where a new keyword is needed.
Sometimes a module will contain a function that returns new objects, where no new keyword is needed.
Usage depends partially on what you are trying to accomplish and partially on your teams coding preference.