获取函数剩余参数

es6语法

const arguments = [1,5,6,8,9,10];
const [context, ...rest] = arguments;
console.log(context) // 1
console.log(rest)  // [ 5, 6, 8, 9, 10 ]

巧用数组的shift方法

function fn (a,b,c,d) {
    
    
  let firstArguments =  Array.prototype.shift.call(arguments)
  console.log(firstArguments) //  1
  console.log(arguments)  // [Arguments] { '0': 2, '1': 3, '2': 4 }
}
fn(1,2,3,4)

巧用扩展符

类数组也可以使用数组的扩展符使其成为数组
在函数参数中两种方式:

const [first, ...reset] = [...arguments];
 [...arguments].slice(1)

猜你喜欢

转载自blog.csdn.net/weixin_43131046/article/details/120487796