Skip to content

Instantly share code, notes, and snippets.

@lylijincheng
Last active December 21, 2015 13:49
Show Gist options
  • Save lylijincheng/6314976 to your computer and use it in GitHub Desktop.
Save lylijincheng/6314976 to your computer and use it in GitHub Desktop.
modules exports.
// A|CMD Module
// Step A: ()()
// Step B: (function() {})(function(){})
// Rock and roll
(function(definition) {
// RequireJS
if (typeof define === 'function') {
define(definition);
} else {
definition();
}
})(function() {
// Define a module
});
(function() {
/* ... Code that defines MyModule ... */
var MyModule = {};
var root;
if (typeof self !== 'undefined') {
// Web Worker
root = self;
} else if (typeof window !== 'undefined') {
// Browser
root = window;
} else if (typeof global !== 'undefined') {
// NodeJS
root = global;
}
root.MyModule = MyModule;
if (typeof module !== 'undefined') {
module.exports = MyModule;
}
})();
// Simplified:
(function(root) {
/* ... Code that defines MyModule ... */
var MyModule = {};
root.MyModule = (root.module || {}).exports = MyModule;
})(this);
// my: exports to window
new function() {
/* ... Code that defines MyModule ... */
var MyModule = {};
this.MyModule = (this.module || {}).exports = MyModule;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment