Pass unknown number of arguments into javascript function

后端 未结 12 1292
情话喂你
情话喂你 2020-12-02 10:17

Is there a way to pass an unknown number of arguments like:

var print_names = function(names) {
    foreach(name in names) console.log(name); // something li         


        
12条回答
  •  既然无缘
    2020-12-02 10:59

    I like to do this:

    This will not help if you don't know the number of arguments, but it helps if you don't want to remember the order of them.

    /**
     *  @param  params.one        A test parameter
     *  @param  params.two        Another one  
     **/
    function test(params) {
    
        var one = params.one;
        if(typeof(one) == 'undefined') {
            throw new Error('params.one is undefined');
        }
    
        var two = params.two;
        if(typeof(two) == 'undefined') {
            throw new Error('params.two is undefined');
        }
    }
    

提交回复
热议问题