Skip to content

Instantly share code, notes, and snippets.

@kriskowal
Forked from creationix/module.js
Created April 19, 2011 18:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriskowal/929059 to your computer and use it in GitHub Desktop.
Save kriskowal/929059 to your computer and use it in GitHub Desktop.
defs = {};
modules = {};
function define(name, fn) {
defs[name] = fn;
}
function require(name) {
console.log("Loading " + name);
if (modules.hasOwnProperty(name)) return modules[name];
if (defs.hasOwnProperty(name)) {
var fn = defs[name];
defs[name] = function () { throw new Error("Circular Dependency"); }
return modules[name] = fn();
}
throw new Error("Module not found: " + name);
}
define('foo', function () {
return 42;
});
define('mymod', function () {
return {
stuff: "goes here",
"and here": require('foo')
};
});
var MyMod = require('mymod');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment