Like most languages, there is more than one way to do this (TMTOWTDI).
function foo(a,b,c){
Function object length method:
foo.length();
Disassemble it (using the test):
args = foo.toString(); RegExp.lastIndex = -1; // reset the RegExp object /^function [a-zA-Z0-9]+\((.*?)\)/.test(args); // get the arguments args = (RegExp.$1).split(', '); // build array: = ["a","b","c"]
This gives you the option to use args.length and a list of actual argument names used in the function definition.
Disassemble it (using replacement):
args = foo.toString(); args = args.split('\n').join(''); args = args.replace(/^function [a-zA-Z0-9]+\((.*?)\).*/,'$1') .split(', ');
Note: This is a basic example. The regular expression should be improved if you want to use it for wider use. Above, function names should only be letters and numbers (not internationalized), and arguments cannot contain any parentheses.
source share