Functions.js
var Functions = (function(String) { //This regex is from require.js var FUNCTION_ARGUMENT_REGEX_PATTERN = /^function\s*[^\(]*\(\s*([^\)]*)\)/m; // protect old browser not support method trim if (!String.prototype.trim) { String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }; } return { /** * regular expression pattern for get arguments name in the function */ FUNCTION_ARGUMENT_REGEX_PATTERN: FUNCTION_ARGUMENT_REGEX_PATTERN, /** * code from http://stackoverflow.com/questions/20058391/javascript-dependency-injection * * @param {Function} func * @throws Error * @returns {Array<String>} - array of arguments name */ getArgumentsName: function(func) { if (typeof func !== 'function') { throw new Error('require parameter as a function'); } var args = func.toString().match(FUNCTION_ARGUMENT_REGEX_PATTERN)[1].split(','); var length = args.length; for (var i = 0; i < length; i++) { args[i] = args[i].trim(); } return args; } }; }).call(this, String);Example to use
function test1(id, name, info){ } function test2(userService){ } function test3(pid, hcode, budgetYear, departmentId){ } console.log(Functions.getArgumentsName(test1)); console.log(Functions.getArgumentsName(test2)); console.log(Functions.getArgumentsName(test3));
Generally I don't learn post on blogs, but I would like to say that this write-up
ตอบลบvery forced me to check out and do so! Your writing
taste has been amazed me. Thanks, very nice post.
Here is my web site SVM-SEO