2

I'm trying to improve my javascript and have been using the revealing module pattern to good effect but I noticed that code in the module not in a function or the return is executed on creation have utilised this, such as:

var MyModule = function ($) {

    setUp();

    function setUp() {
     // execute code to be run when module created
    }

    function doStuff() {
    }

    return {
        doStuff: doStuff
    }
}

I can see this is creating a form of constructor for the function which relies on it being called by var myModule = new MyModule(jQuery).

Any feedback on the rights or wrongs of this appreciated.

Nathan
  • 931
  • 1
  • 12
  • 26
  • 3
    this is better suited for codereview.stackexchange.com – Daniel A. White Apr 09 '13 at 14:06
  • 3
    Your question is really broad. One thing I'll point out is that using `new` with your `MyModule` function is misleading. You can (and should) just call it without `new`, because it returns an object (and therefore the object created by `new` gets thrown away). Also, normally a module like this is defined by having the function call itself immediately, rather than waiting for someone else to call it later. Finally, you said the "...code in the module not in a function..." but it **is** in a function (`MyModule`). – T.J. Crowder Apr 09 '13 at 14:07
  • 1
    Note: the suggestion from @DanielA.White seem correct, but you *don't need to re-post the question there*. I've flagged this question for migration to codereivew.SE, so if the mods think it belongs there, it will be moved there. – apsillers Apr 09 '13 at 14:08
  • Thanks to T J Crowder for your reply and I'll wait for it to appear on codereview. – Nathan Apr 09 '13 at 14:31

0 Answers0