Skip to content

Instantly share code, notes, and snippets.

@rauschma
Created October 30, 2012 21:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rauschma/3983278 to your computer and use it in GitHub Desktop.
Save rauschma/3983278 to your computer and use it in GitHub Desktop.
Universal Module Definition pattern
// Related: UMD, a project cataloging UMD patterns https://github.com/umdjs/umd
// Universal module definition: requires either an AMD loader or Node.js
({ define: typeof define === 'function'
? define
: function (names, body) {
module.exports = body.apply(null, names.map(require));
}
}).
define(["foo", "bar"], function (foo, bar) {
return {
// ...
};
});
// Rationales:
// - Keep light, only support AMD on browsers
// - The UMD boilerplate is only a prefix
// => easier to add to AMDs
// => easier to see what the actual module is
// - I consider define(...) above to be the core AMD syntax, which is why I use it here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment