JavaScript variable number of arguments to function

后端 未结 11 645
太阳男子
太阳男子 2020-11-22 02:14

Is there a way to allow \"unlimited\" vars for a function in JavaScript?

Example:

load(var1, var2, var3, var4, var5, etc...)
load(var1)
11条回答
  •  借酒劲吻你
    2020-11-22 02:32

    It is preferable to use rest parameter syntax as Ramast pointed out.

    function (a, b, ...args) {}
    

    I just want to add some nice property of the ...args argument

    1. It is an array, and not an object like arguments. This allows you to apply functions like map or sort directly.
    2. It does not include all parameters but only the one passed from it on. E.g. function (a, b, ...args) in this case args contains argument 3 to arguments.length

提交回复
热议问题