How to get function parameter names/values dynamically?

前端 未结 30 3156
说谎
说谎 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:27

    Whatever the solution, it must not break on wierd functions, whose toString() looks just as wierd:

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

    screenshot from console

    Also, why use complex regular expressions? This can be done like:

    function getArguments(f) {
        return f.toString().split(')',1)[0].replace(/\s/g,'').substr(9).split(',');
    }
    

    This works everywhere with every function, and the only regex is whitespace removal that doesn't even process the whole string due to the .split trick.

提交回复
热议问题