This new version handles fat arrow functions as well...
args = f => f.toString ().replace (/[
s]+/g, ' ').
match (/(?:functions*w*)?s*(?:((.*?))|([^s]+))/).
slice (1,3).
join ('').
split (/s*,s*/);
function ftest (a,
b,
c) { }
let aftest = (a,
b,
c) => a + b / c;
console.log ( args (ftest), // = ["a", "b", "c"]
args (aftest), // = ["a", "b", "c"]
args (args) // = ["f"]
);
Here is what I think you are looking for :
function ftest (a,
b,
c) { }
var args = ftest.toString ().
replace (/[
s]+/g, ' ').
match (/functions*w*s*((.*?))/)[1].split (/s*,s*/);
args will be an array of the names of the arguments of test i.e. ['a', 'b', 'c']
The value is args will be an array of the parameter names if the ftest
is a function.
The array will be empty if ftest
has not parameters. The value of args
will be null
if ftest
fails the regular expression match, i.e it is not a function.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…