Skip to content

Instantly share code, notes, and snippets.

@sofish
Created August 4, 2013 11:25
Show Gist options
  • Save sofish/6150083 to your computer and use it in GitHub Desktop.
Save sofish/6150083 to your computer and use it in GitHub Desktop.
利用 argument 来做依赖注入,一个简单的例子
var obj = {
hello: function() {
console.log('sofish');
},
world: function(){
console.log('lin');
}
};
var fn = function(hello, ooxx, world){
var di = arguments.callee
.toString()
.replace(/^[^(]+\(([^)]+)\)(?:.+(\n)?)+$/, '$1')
.replace(/\s/g, '')
.split(',');
for(var i = 0, len = di.length; i < len; i++) {
var p = di[i];
if(typeof obj[p] !== 'undefined') obj[p]();
}
}
fn();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment