How to get a slice from “arguments”

后端 未结 7 1955
天命终不由人
天命终不由人 2020-12-01 09:10

All you know that arguments is a special object that holds all the arguments passed to the function.

And as long as it is not an array - you cannot use

7条回答
  •  孤城傲影
    2020-12-01 09:23

    You can use ...rest within the function to separate the first and the rest of the arguments:

    function foo(arr) {
      const [first, ...rest] = arguments;
      console.log(`first = ${first}`);
      console.log(`rest = ${rest}`);
    }
    //Then calling the function with 3 arguments:
    foo(1,2,3)

提交回复
热议问题