How to get function parameter names/values dynamically?

前端 未结 30 3225
说谎
说谎 2020-11-22 00:13

Is there a way to get the function parameter names of a function dynamically?

Let’s say my function looks like this:

function doSomething(param1, par         


        
30条回答
  •  庸人自扰
    2020-11-22 00:38

    I have read most of the answers here, and I would like to add my one-liner.

    new RegExp('(?:'+Function.name+'\\s*|^)\\((.*?)\\)').exec(Function.toString().replace(/\n/g, ''))[1].replace(/\/\*.*?\*\//g, '').replace(/ /g, '')
    

    or

    function getParameters(func) {
      return new RegExp('(?:'+func.name+'\\s*|^)\\s*\\((.*?)\\)').exec(func.toString().replace(/\n/g, ''))[1].replace(/\/\*.*?\*\//g, '').replace(/ /g, '');
    }
    

    or for a one-liner function in ECMA6

    var getParameters = func => new RegExp('(?:'+func.name+'\\s*|^)\\s*\\((.*?)\\)').exec(func.toString().replace(/\n/g, ''))[1].replace(/\/\*.*?\*\//g, '').replace(/ /g, '');
    

    __

    Let's say you have a function

    function foo(abc, def, ghi, jkl) {
      //code
    }
    

    The below code will return "abc,def,ghi,jkl"

    That code will also work with the setup of a function that Camilo Martin gave:

    function  (  A,  b
    ,c      ,d
    ){}
    

    Also with Bubersson's comment on Jack Allan's answer:

    function(a /* fooled you)*/,b){}
    

    __

    Explanation

    new RegExp('(?:'+Function.name+'\\s*|^)\\s*\\((.*?)\\)')

    This creates a Regular Expression with the new RegExp('(?:'+Function.name+'\\s*|^)\\s*\\((.*?)\\)'). I have to use new RegExp because I am injecting a variable (Function.name, the name of the function being targeted) into the RegExp.

    Example If the function name is "foo" (function foo()), the RegExp will be /foo\s*\((.*?)\)/.

    Function.toString().replace(/\n/g, '')

    Then it converts the entire function into a string, and removes all newlines. Removing newlines helps with the function setup Camilo Martin gave.

    .exec(...)[1]

    This is the RegExp.prototype.exec function. It basically matches the Regular Exponent (new RegExp()) into the String (Function.toString()). Then the [1] will return the first Capture Group found in the Regular Exponent ((.*?)).

    .replace(/\/\*.*?\*\//g, '').replace(/ /g, '')

    This will remove every comment inside /* and */, and remove all spaces.


    This also now supports reading and understanding arrow (=>) functions, such as f = (a, b) => void 0;, in which Function.toString() would return (a, b) => void 0 instead of the normal function's function f(a, b) { return void 0; }. The original regular expression would have thrown an error in its confusion, but is now accounted for.

    The change was from new RegExp(Function.name+'\\s*\\((.*?)\\)') (/Function\s*\((.*?)\)/) to new RegExp('(?:'+Function.name+'\\s*|^)\\((.*?)\\)') (/(?:Function\s*|^)\((.*?)\)/)


    If you want to make all the parameters into an Array instead of a String separated by commas, at the end just add .split(',').

提交回复
热议问题