ES6 使用技巧
解构赋值
const obj={code:200,ok:true};
const {code,ok}=obj;
const {ok:success}=obj;
console.log(code,ok,success);
const arr=['一','二'];
const [first,second]=arr;
console.log(first,second);
合并数组或对象
const a = [1,2,3];
const b = [1,5,6];
const c = [...a,...b];//[1,2,3,5,6]
const obj1 = {
a:1,
}
const obj2 = {
b:1,
}
const obj = {...obj1,...obj2};//{a:1,b:1}
判断包含
if([1,2,3,4].includes(2) ){
}
if("我是中国人".includes("中国") ){
}
非空的判断
if(target??'' !== ''){
}
遍历字符串
const s='hello world!';
for (item of s){
console.log(item);
}
字符串模板
const name = '刘德华';
const age = 34;
const result = `${name} 看起来很 ${age>50?'老':'年轻'}`;