How to get function parameter names/values dynamically?

前端 未结 30 2645
说谎
说谎 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条回答
  •  梦毁少年i
    2020-11-22 00:29

    You can access the argument values passed to a function using the "arguments" property.

        function doSomething()
        {
            var args = doSomething.arguments;
            var numArgs = args.length;
            for(var i = 0 ; i < numArgs ; i++)
            {
                console.log("arg " + (i+1) + " = " + args[i]);  
                        //console.log works with firefox + firebug
                        // you can use an alert to check in other browsers
            }
        }
    
        doSomething(1, '2', {A:2}, [1,2,3]);    
    

提交回复
热议问题