Created
November 28, 2017 05:53
-
-
Save capfsb/87c672ff437c713c2223ca958b4716d3 to your computer and use it in GitHub Desktop.
DI v.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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