Is it possible to send a variable number of arguments to a JavaScript function?

后端 未结 12 905
[愿得一人]
[愿得一人] 2020-11-22 10:51

Is it possible to send a variable number of arguments to a JavaScript function, from an array?

var arr = [\'a\',\'b\',\'c\']

var func = function()
{
    //         


        
12条回答
  •  萌比男神i
    2020-11-22 11:10

    With ES6 you can use rest parameters for varagrs. This takes the argument list and converts it to an array.

    function logArgs(...args) {
        console.log(args.length)
        for(let arg of args) {
            console.log(arg)
        }
    }
    

提交回复
热议问题