Created
January 5, 2016 13:59
-
-
Save paranoidjk/41b306937b2a5917b4de to your computer and use it in GitHub Desktop.
define a module which can use in Node, AMD, CMD and Browser by warp the code in a closure.
This file contains hidden or 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
| ;(function (name, definition) { | |
| // test if the context is AMD or CMD | |
| var hasDefine = typeof define === 'function', | |
| // test if the context is Node | |
| hasExports = typeof module !== 'undefined' && module.exports; | |
| if (hasDefine) { | |
| // AMD or CMD | |
| define(definiton); | |
| } else if (hasExports) { | |
| // Node | |
| module.exports = definiton(); | |
| } else { | |
| // in browser, this point to object `window` | |
| this[name] = definition(); | |
| } | |
| })('hello', function() { | |
| var hello = function() {}; | |
| return hello; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment