-
-
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
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
(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