Skip to content

Instantly share code, notes, and snippets.

@tmathews
Last active August 29, 2015 14:02
Show Gist options
  • Save tmathews/13d929c48e39e2d49c29 to your computer and use it in GitHub Desktop.
Save tmathews/13d929c48e39e2d49c29 to your computer and use it in GitHub Desktop.
// Define modules.
need("cookie", function ( need ){
function Cookie ( type ) {
this.type = type;
}
Cookie.prototype.getName = function () {
return this.type + " Cookie";
}
return Cookie;
});
need("generate-cookie", function ( need ) {
var Cookie = need("cookie");
return function generateCookie () {
return new Cookie("Chocolate Chip");
}
});
// Define need function.
function need ( key, value ) {
if ( !need.cache ) need.cache = {};
if ( value ) {
need.cache[key] = { create: value };
return;
}
var obj = need.cache[key];
if ( !obj ) return;
if ( !obj.value ) obj.value = obj.create(need);
return obj.value;
}
// Use it.
var generator = need("generate-cookie");
var cookie = generator();
console.log(cookie.getName());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment