Skip to content

Instantly share code, notes, and snippets.

@wilsonpage
Created March 3, 2012 17:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilsonpage/1967125 to your computer and use it in GitHub Desktop.
Save wilsonpage/1967125 to your computer and use it in GitHub Desktop.
How to define a module for both AMD and CommonJS compatibility
/**
* Registers module as CommonJS or AMD if support is found
* and creates an exports object for the module to use
*
* @param {boolean} cjs
* @param {boolean} amd
* @param {Object} ex
* @return {Object}
*/
var exports = (function(cjs, amd, ex){
if (cjs) module.exports = ex; else if (amd) define(ex); return ex;
})(typeof module == 'object', typeof define === 'function', {});
// example of private method
var privateMethod = function(){
console.log('Hello inside world');
};
// example of public method
exports.publicMethod = function(){
console.log('Hello outside world');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment