展开运算符

把对象或者数组里面的东西直接打开,一般多用于数组里面

进行数组的拼接

//数组拼接
var arr1=[4,5,6];
var arr2=[7,8,9];
var arr3 =[1,2,3,...arr1,...arr2];

console.log(arr3); //[1,2,3,4,5,6,7,8,9]

进行函数的参数传递

var arr3 = [1,2,3,4,5,6,7,8,9]
var res = Math.max(...arr3);
console.log(res);//9

展开对象

var obj ={
name:"中国",
age:5000
}

var obj2 ={
...obj,
nation:56
}

//obj2等价于:{name:"中国",age:5000,nation:56}
let person = {name:'tom',age:18}
let person2 = {...person} //克隆 react +bable
console.log(person2); //{name: 'tom', age: 18}
//合并
let person3 = {...person,name:'jack',address: 18}

console.log(person3); //{name: 'jack', age: 18}

合并运算符

写在形参位置的时候是合并

会把所有形参组和在一个数组里面返回

function fun(...arg){
console.log(arg);// [1,2,3]
}
fun(1,2,3);//[1,2,3]

function fun2(a,...arg){
console.log(a); // 1
console.log(arg); // [2,3]
}
fun2(1,2,3);

写在解构数组的时候是合并

var arr=[1,2,3,4,5,6];
let [a,...b]=arr;
console.log(a); // 1
console.log(b); // [2,3,4,5,6]