rest和spread操作符

Rest和Spread操作符:

作用:用来声明任意数量的方法参数

//这三个点点点 指的是,调用这个方法的时候,可以传任意数量的参数进来

function fun1(...args){

  args.foreach(function(arg){

    console.log(arg)

    })

}

fun1(1,2);

fun1(1,2,3);

//result

1

2

1

2

3

作用2:可以将一个任意长度的参数转化为固定的长度

function fun2(a,b,c){

  console.log(a);

  console.log(b);

  console.log(c);

}

var arg1 = [1,2];

fun2(...arg1);

var arg2 = [7,8,9,10];

fun2(...arg2);

//result

1

2

undefined

7

8

9

其中第二种方法在typescript中还暂不支持,但在es6中可以

原文地址:https://www.cnblogs.com/maochunyan/p/9361339.html