Skip to content

Instantly share code, notes, and snippets.

@jfsiii
Forked from unscriptable/another-way.js
Created May 13, 2011 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfsiii/969908 to your computer and use it in GitHub Desktop.
Save jfsiii/969908 to your computer and use it in GitHub Desktop.
AMD-compatible CommonJS Module/1.1 that can also be loaded in a script tag
(function () {
/*!
* expose.js
*
* @author Oleg Slobodskoi
* @website https://github.com/kof/expose.js
* @licence Dual licensed under the MIT or GPL Version 2 licenses.
*/
function expose(namespace, api) {
var env = {};
if (typeof namespace !== 'string') {
api = namespace;
namespace = null;
}
// the global api of any environment
// thanks to Nicholas C. Zakas
// http://www.nczonline.net/blog/2008/04/20/get-the-javascript-global/
env.global = (function(){
return this;
}).call(null);
// expose passed api as exports
env.exports = api || {};
// commonjs
if (typeof module !== 'undefined' &&
typeof exports !== 'undefined' &&
module.exports) {
env.commonjs = true;
env.module = module;
module.exports = exports = env.exports;
}
// browser only
if (typeof window !== 'undefined') {
env.browser = true;
// we are not in amd wrapper
if (!env.commonjs && namespace && env.exports) {
env.global[namespace] = env.exports;
}
}
return env;
}
// My module
// Constructor
function Module( val ){ this.prop = val; }
// Static / "Class" Methods
Module.do = function( something ){ console.log('Module.do(',something,')'); };
// Instance methods
Module.prototype = {
method = function(){ console.log(arguments); },
secret = 42
};
expose('Module', Module);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment