Created
October 30, 2012 21:44
-
-
Save rauschma/3983278 to your computer and use it in GitHub Desktop.
Universal Module Definition pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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