Unlimited arguments in a JavaScript function

前端 未结 9 1852

Can a JavaScript function take unlimited arguments? Something like this:

testArray(1, 2, 3, 4, 5...);

I am trying:

var arr          


        
相关标签:
9条回答
  • 2020-12-07 18:22

    There are some legacy methods but I prefer the ES6 and newer versions, So if I wanna implement this, I wrote it like below:

    const func = ...arg => console.log(arg);
    

    Simple and cutting edge of tech.

    0 讨论(0)
  • 2020-12-07 18:23
    function toArray() {
       return arguments;
    }
    
    var myargs = toArray(1, 2, 3, 4, 5, 6);
    

    The arguments keyword is available in every js function

    0 讨论(0)
  • 2020-12-07 18:32
    var arr = [];
    function testArray() {
        Array.prototype.push.apply(arr, arguments);
    }
    
    0 讨论(0)
提交回复
热议问题