js实现依赖注入

function a(){
  console.log("a");
}

function b () {
  console.log("b");
}

function c () {
  console.log("c");
}

var injector = {
  dependencies: {},
  register: function(key, value) {
     this.dependencies[key] = value;
  },
  resolve: function(deps, func, scope) {
     var func, deps, scope, args = [], self = this;

     func = arguments[0];
     deps = func.toString().match(/^function\s*[^\(]*\(\s*([^\)]*)\)/m)[1].replace(/\s/g, '').split(',');
     scope = arguments[1] || {};

     return function() {
        var a = Array.prototype.slice.call(arguments, 0);

        for(var i=0; i<deps.length; i++) {
           var d = deps[i];
           args.push(self.dependencies[d] && d != '' ? self.dependencies[d] : a.shift());
        }

        func.apply(scope || {}, args);
     }
  }
}

injector.register("a", a);
injector.register("b", b);

injector.resolve(function (b, a, c) {
  a();
  b();
  c();
})(c);