赞
踩
… 运算符:是ES6里一个新引入的运算法, 也叫展开/收集运算符。
用法一 :展开
const a=[2,3,4]
const b=[1,…a,5];
console.log(b); //[1,2,3,4,5]
用法二 :收集
function fn(a,b,…c){
console.log(a,b,c);
}
fn(1,2,3,4,5); //1,2 [ 3, 4, 5 ]
用法三 :为数组新增成员
const a=[“张三”,“李四”];
const b =“王五”;
const all = […a,b];
console.log(all); //[ ‘张三’, ‘李四’, ‘王五’ ]
用法四 :为对象新增属性
const obj = { name: ‘jack’, age: 30 }
const result = { …obj, sex: ‘男’, height: ‘178cm’ }
console.log(result); // {name: “jack”, age: 30, sex: “男”, height: “178CM”}
用法五 : 合并数组和数组对象
const a = [1, 2, 3];
const b = [4, 5, 6];
const result = […a, …b]; // [1, 2, 3, 4, 5, 6]
用法六 :合并对象(相同的属性会覆盖掉)
const people = {
name: ‘Lucy’,
age: 30,
sex: ‘女’
};
const base = {
age: 22,
job: ‘teacher’
}
const all = { …people, …base };
console.log(all); // {name: “Lucy”, age: 22, sex: “女”, job: “teacher”}
就先写到这里吧!!高级用法还没学明白呢,但是【…】它确实挺强大的
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。