js中剩余参数

JavaScript中的剩余参数是指函数定义时以...为前缀的参数,这个参数是一个没有对应形参的实参组成的一个数组,所以它与arguments有一定的区别,剩余参数中可以使用push、map等数组方法
function
num(a,b,...arg){ console.log(arg); arg.push(a,b); console.log(arg); } num(1,2,3,4,5);//[3,4,5] [3,4,5,1,2]

因为使用方法<code>functionName.length</code>可以获取函数对应的形参个数,其中functionName.length为对应的函数名,所以可以用arguments模拟剩余参数
function arg(a,b){
      console.log(arg.length);
      var newArg=Array.prototype.slice.call(arguments,arg.length);
      console.log(newArg);
}
arg(1,2,3,4,5);//2   [3,4,5]

转载:https://www.jianshu.com/p/f99a56a5e097

原文地址:https://www.cnblogs.com/huanhuan55/p/11947491.html