1

I'm trying to grab an array of string represented parameters from a function and I'm unsure how to proceed. Basically given the function below

function MyFunc(param1, param2, param3){
  //do all the things
}

What would the function "getParams" look like to do the following

getParams(MyFunc) // ["param1","param2","param3"]
The Process
  • 5,913
  • 3
  • 30
  • 41
frostme
  • 113
  • 2
  • 8

1 Answers1

0

This is a bit messy, but you can do this by converting your function to a string and then splitting it until you get just the parameters:

var getFuncParams = function(MyFunc) {
    var str = MyFunc.toString()
    var strParams = str.substr((str.indexOf('(')+1), (str.indexOf(')') - str.indexOf('(')) - 1)
    var params = strParams.split(",")
    return params;
}
millerbr
  • 2,951
  • 1
  • 14
  • 25