Created
March 3, 2012 17:55
-
-
Save wilsonpage/1967125 to your computer and use it in GitHub Desktop.
How to define a module for both AMD and CommonJS compatibility
This file contains 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
/** | |
* 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