[removed] “Infinite” parameters for function?

后端 未结 3 1097
情话喂你
情话喂你 2021-01-31 14:52

In Chrome, when I type console.log in the one below:

console.log(\"A parameter\", \"A parameter\", \"A parameter\", \"A parameter\", \"A parameter\"         


        
3条回答
  •  面向向阳花
    2021-01-31 15:37

    The modern way of doing this is using rest parameters:

    function printArguments(...args) {
      args.forEach((arg, index) => {
        console.log(`Argument ${index}:`, arg);
      });
    }
    
    printArguments('hello', true, new Date());
    

    By using the ...args syntax, all parameters are saved in an array named args.

    Except of Internet Explorer, all browsers already ship this feature in their newest version.

    Fiddle: https://jsfiddle.net/Lbf0stst/

提交回复
热议问题