Created
August 28, 2014 12:40
-
-
Save Gwash3189/85829a0c45f5bdcc6c9b to your computer and use it in GitHub Desktop.
// source http://jsbin.com/qogujoyadome/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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script> | |
window.xinj = function(obj) { | |
debugger; | |
var api = { | |
bag: {}, | |
start: function() { | |
Object.keys(api.bag).forEach(function(value){ | |
debugger; | |
api.bag[value].fn.apply(api.bag[value].fn, api.bag[value]); | |
}) | |
}, | |
getFuncsArgs: function(func) { | |
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; | |
var ARGUMENT_NAMES = /([^\s,]+)/g; | |
var fnStr = func.toString().replace(STRIP_COMMENTS, '') | |
var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES) | |
if(result === null){ | |
result = [] | |
} | |
return result; | |
} | |
}; | |
var findFuncsRecursive = function(thing) { | |
var result; | |
if(typeof thing !== "object"){ | |
return [thing]; | |
} else { | |
result = Object.keys(thing).map(function(value){ | |
return findFuncsRecursive(thing[value]) | |
}); | |
result.forEach(function(value, i) { | |
if(value instanceof Array){ | |
debugger; | |
result.splice(i,1); | |
result.push({ | |
func: value[i], | |
args: api.getFuncsArgs(value[i]) | |
}); | |
} else { | |
result.push({ | |
func: value, | |
args: api.getFuncsArgs(value) | |
}); | |
} | |
}); | |
return result; | |
} | |
} | |
Object.keys(obj).forEach(function(value){ | |
findFuncsRecursive(obj[value]).forEach(function(tup) { | |
api.bag[value] = {fn: tup.func, args: tup.args}; | |
}); | |
}); | |
return api; | |
} | |
window.Expense = {}; | |
window.Expense.test = function(){}; | |
window.Expense.Dom = {}; | |
window.Expense.Dom.link = function() { | |
console.log("run"); | |
} | |
debugger; | |
xinj(Expense).start(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment