Run JSLint over the code
After the issues highlighted by JSLint have been fixed, organise the code so that it follows a more structured outline such as having a collection of vars and functions.
Add the AMD define call
For it to be recognised by module loaders such as require.js, the code needs to be wrapped in define calls which will turn it into an AMD module.
Dependencies such as jQuery
To add any dependencies such as jQuery, simply include them in the define call array such as this:-
Any dependencies added in this way also need to be AMD compatible modules or a shim might be needed, documentation for jQuery can be found here:- http://requirejs.org/docs/jquery.html
Add a little Encapsulation
One of the advantages of the module pattern is that it can be structured so that variables and functions can be internalised and effectively defined in a 'private' scope. The module's return object can define exactly what should be publicly accessible. In the previous code snippet, the variable
foo and both functions were all returned by the module. This means that they would all be publicly accessible by any client code. Instead, we want to ensure that
foo and the functions are initially defined as private, and then expose the functions explicitly in the return object.