Skip to content

Instantly share code, notes, and snippets.

@jmgunn87
Last active August 29, 2015 13:57
Show Gist options
  • Save jmgunn87/9611981 to your computer and use it in GitHub Desktop.
Save jmgunn87/9611981 to your computer and use it in GitHub Desktop.
dependency injection container
function Container() {
this.s = {};
}
Container.prototype = {
put: function (i, v, o) {
return this.s[i] = !o ? v : function (p) {
return this.c = this.c || v(p);
};
},
get: function (i, p) {
return typeof this.s[i] === 'function' ?
this.s[i](p) : this.s[i];
},
del: function (i) {
return delete this.s[i];
}
};
var c = new Container ();
c.put("a", function (p) { return p.name; }, true);
c.put("b", function (p) { return p.name; });
alert(c.get("a", { name: 'a1' }));
alert(c.get("a", { name: 'a2' }));
alert(c.get("b", { name: 'b1' }));
alert(c.get("b", { name: 'b2' }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment