Get function parameter names for interface purposes

前端 未结 5 1276
灰色年华
灰色年华 2021-01-01 03:36

can anyone help me on how to retrieve function parameter names? For example:

var A = function(a, b, c) {};

I need to get the parameter name

5条回答
  •  情话喂你
    2021-01-01 04:07

    Just to make complete @jerjer answer, We may also ensure there is at least one match:

    function getFnParamNames(fn){
      const match = fn.toString().match(/\(.*?\)/) 
      return match ? match[0].replace(/[()]/gi,'').replace(/\s/gi,'').split(',') : [];
    }
    

提交回复
热议问题