How to get function parameter names/values dynamically?

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

    I've tried doing this before, but never found a praticial way to get it done. I ended up passing in an object instead and then looping through it.

    //define like
    function test(args) {
        for(var item in args) {
            alert(item);
            alert(args[item]);
        }
    }
    
    //then used like
    test({
        name:"Joe",
        age:40,
        admin:bool
    });
    

提交回复
热议问题