Skip to content

Instantly share code, notes, and snippets.

@husa
Last active December 18, 2015 02:38
Show Gist options
  • Save husa/5712005 to your computer and use it in GitHub Desktop.
Save husa/5712005 to your computer and use it in GitHub Desktop.
how to declare a global variable or export it as CJS or AMD module if posible. 'this' must refer to global object or you can put this code into global scope
// myModule - is our global we want to export
var root = this;
if (typeof(exports) !== 'undefined') {
if (typeof(module) !== 'undefined' && module.exports) {
exports = module.exports = myModule;
}
exports.myModule = myModule;
} else {
if (typeof define === 'function' && define.amd) {
define('myModule', [], function(){ return myModule; });
}
root.myModule = myModule;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment