ES6对象展开运算符

数组的应用

var arr1 = [1,2,3,4];
var arr2 = [5,6,7,8];
var arr3 = [...arr1,...arr2];
var arr4 = [...arr1,88,99];
var arr5 = [...arr1,88,99,...arr2];
var arr6 = [...arr1,88,99,...arr2,11,22,...arr1];
console.log(arr1);  //[1, 2, 3, 4]
console.log(arr2);  //[5, 6, 7, 8]
console.log(arr3);  //[1, 2, 3, 4, 5, 6, 7, 8]
console.log(arr4);  //[1, 2, 3, 4, 88, 99]
console.log(arr5);  //[1, 2, 3, 4, 88, 99, 5, 6, 7, 8]
console.log(arr6);  //[1, 2, 3, 4, 88, 99, 5, 6, 7, 8, 11, 22, 1, 2, 3, 4]

对象的应用

var obj  = {a:1,b:2}
var obj1 = {c:3,d:4}
var obj2 = {...obj,...obj1}
var obj3 = {...obj,a:8,w:66}
var obj4 = {...obj,a:8,w:66,...obj1}
console.log(obj);   //{a: 1, b: 2}
console.log(obj1);  //{c: 3, d: 4}
console.log(obj2);  //{a: 1, b: 2, c: 3, d: 4}
console.log(obj3);  //{a: 8, b: 2, w: 66}
console.log(obj4);  //{a: 8, b: 2, w: 66, c: 3, d: 4}

总结:无论数组还是对象,都是不改变原来的值。

posted @ 2021-06-08 21:08  smil、梵音  阅读(358)  评论(0编辑  收藏  举报