How to get function parameter names/values dynamically?

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

    How I typically do it:

    function name(arg1, arg2){
        var args = arguments; // array: [arg1, arg2]
        var objecArgOne = args[0].one;
    }
    name({one: "1", two: "2"}, "string");
    

    You can even ref the args by the functions name like:

    name.arguments;
    

    Hope this helps!

提交回复
热议问题