Skip to content

Instantly share code, notes, and snippets.

@capfsb
Created November 28, 2017 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capfsb/87c672ff437c713c2223ca958b4716d3 to your computer and use it in GitHub Desktop.
Save capfsb/87c672ff437c713c2223ca958b4716d3 to your computer and use it in GitHub Desktop.
DI v.1
var binds = {
'SquareCalc': ['Multiplicator'],
'A': ['SquareCalc'],
'B': ['Multiplicator']
};
var registry = {};
function make(cls) {
if (registry[cls]) {
return registry[cls]
}
var deps = [null];
for (var i in binds[cls]) {
deps.push(make(binds[cls][i]));
}
var instance = new (Function.prototype.bind.apply(window[cls], deps));
registry[cls] = instance;
return instance;
}
function Calc() {
this.plus = function(a, b) {
return +a + +b
}
}
function SquareCalc(multi) {
this.multi = multi;
this.plus = function(a, b) {
return multi.run(a) + multi.run(b)
}
}
function Multiplicator() {
this.run = function(a) {
return a * a;
}
}
function A(calc) {
this.calc = calc;
}
function B() {}
var a = make('A');
var b = make('B');
a.calc.plus(10, 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment