Skip to content

Instantly share code, notes, and snippets.

@milesplit
Created November 28, 2011 08:33
Show Gist options
  • Save milesplit/1399624 to your computer and use it in GitHub Desktop.
Save milesplit/1399624 to your computer and use it in GitHub Desktop.
really really basic common js
below i'm just mocking up a really simple way to emmulate module/require code similar to commonjs
i just threw this together and it doesnt do any loading at all... have another script i could strap that to
wonder though if this should be async instead of sync, even though commonjs is synchronous with require
<script>
window.__loadedModules = {};
window.define = function(n,f){
var exports = {};
f(exports);
__loadedModules[n] = exports;
};
window.require = function(n){ return __loadedModules[n]; };
// equivalent of external module
define('hello world', function(exports){
exports.hello = 'world';
});
// require it
var hello = require('hello world');
console.log(hello);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment