Skip to content

Instantly share code, notes, and snippets.

@paranoidjk
Created January 5, 2016 13:59
Show Gist options
  • Select an option

  • Save paranoidjk/41b306937b2a5917b4de to your computer and use it in GitHub Desktop.

Select an option

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.
;(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