Skip to content

Instantly share code, notes, and snippets.

@robatron
Created February 28, 2014 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robatron/9276675 to your computer and use it in GitHub Desktop.
Save robatron/9276675 to your computer and use it in GitHub Desktop.
/** Example module that supports CommonJS and IFFE modularity
*
* - CommonJS modules can be imported via `require`
* - IFFE modules attach themselves to the browser's window object
*/
;( function ( factory ) {
// If the environment supports CommonJS modules, attach module to `module.exports`
if ( typeof require === 'function' && typeof exports === 'object' && typeof module === 'object' ) {
var target = module.exports || exports;
factory( target );
// Otherwise, attach module to the window
} else {
window.myModule = {};
factory( window.myModule );
}
}( function ( target ) {
// Exposed module
target.foo = 'bar';
// ...
} ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment