//依赖注入

var DI = function(dependency){
    this.dependency = dependency;
}

DI.prototype.inject = function(func){
  var funcString = func.toString();
  var reg = /function\s*\(([^\)]+)\)/;
  var argString = funcString.match(reg);
  var args = argString[1].replace(/\s+/g,'').split(',');
  var newArgs = [];
  var self = this;
  args.forEach(function(_arg){
      if(self.dependency[_arg]){
          newArgs.push(self.dependency[_arg]);
      }
  })
  return function(){
      return func.apply(null, newArgs);
  }
}

var deps = {
    'dep1': function(){
        return 'this is dep1'
    },
    'dep2': function(){
        return 'this is dep2'
    },
    'dep3': function(){
        return 'this is dep3'
    },
    'dep4': function(){
        return 'this is dep4'
    }
}

var di = new DI(deps);

var myFunc = di.inject(function(dep3, dep1, dep2){
    return [dep1(), dep2(), dep3()].join(' -> ')
})

var a  = myFunc();
console.log(a);