一、对数组进行操作(es6)
1.进行数组的复制
{
const list = ['a','b','c','d'];
let list1 = [...list];
list.unshift('e');
console.log(list1);
}
2.进行数组的分割
{
const list = ["a", "b", "c", "d"];
let [, ...list1] = list;
console.log(list1);
}
3.传递方法的参数(对数组进行展开)
{
let list = ["a", "b"];
function concat(a,b){
return a+b;
}
console.log(concat(...list));
}
二、对对象的操作(es8)
1.配置准备
npm install babel-plugin-transform-object-rest-spread
{
"presets": ["es2015"],
"plugins": ["transform-object-rest-spread"]
}
2.可以复制对象
{
const obj = {
name: "Jack",
age: 18,
};
let obj1 = { ...obj };
console.log(obj1);
}
3.设置对象默认值
{
const obj = {
name: "Jack",
age: 18,
};
let obj2 = { ...obj, name: "Marry" };
console.log(obj2);
}
4.合并对象
{
const obj = {
name: "Jack",
age: 18,
};
const addr = { addr: "zh" };
let obj3 = { ...obj, ...addr };
console.log(obj3);
}
注意
- 扩展运算符对对象的复制是浅拷贝(复制的是对象指针),意味着源对象一旦改变,当前拷贝的对象也会改变。
- 对简单类型的拷贝不会产生影响,源对象改变不会对当前对象产生影响。