1、解构赋值
例如:有个对象obj
const obj = {
a:1,
b:2,
c:3,
d:4,
e:5,
}
普通取值:
const a = obj.a;
const b = obj.b;
const c = obj.c;
const d = obj.d;
const e = obj.e;
//或者
const f = obj.a + obj.d;
const g = obj.c + obj.e;
ES6取值(解构赋值)
const {a,b,c,d,e} = obj;
const f = a + d;
const g = c + e;
2、合并数据
普通的合并
//合并数组
const a = [1,2,3];
const b = [1,5,6];
const c = a.concat(b);//[1,2,3,1,5,6]
//合并对象
const obj1 = {
a:1,
}
const obj2 = {
b:1,
}
const obj = Object.assign({}, obj1, obj2);//{a:1,b:1}
ES6合并
//合并数组
const a = [1,2,3];
const b = [1,5,6];
const c = [...new Set([...a,...b])];//[1,2,3,5,6]
//合并对象
const obj1 = {
a:1,
}
const obj2 = {
b:1,
}
const obj = {...obj1,...obj2};//{a:1,b:1}
3、拼接字符串
普通拼接字符串
const name = '小明';
const score = 59;
let result = '';
if(score > 60){
result = `${name}的考试成绩及格`;
}else{
result = `${name}的考试成绩不及格`;
}
ES6拼接
const name = '小明';
const score = 59;
const result = `${name}${score > 60?'的考试成绩及格':'的考试成绩不及格'}`;