How to get a slice from “arguments”

后端 未结 7 1963
天命终不由人
天命终不由人 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:25

    Meddling with array functions is not actually necessary.

    Using rest parameter syntax ...rest is cleaner and more convenient.

    Example

    function argumentTest(first, ...rest) {
      console.log("First arg:" + first);
    
      // loop through the rest of the parameters
      for (let arg of rest) {
        console.log("- " + arg);
      }
    }
    
    // call your function with any number of arguments
    argumentTest("first arg", "#2", "more arguments", "this is not an argument but a contradiction");

    ...Rest

    • See the example Fiddle
    • See MDN Documentation page

提交回复
热议问题