ES6 用例

解构赋值

const obj = {
    a:1,
    b:2,
    c:3,
    d:4,
    e:5,
}

// 解构的对象不能为undefined、null
const {a, b, c, d, e} = obj || {}
const f = a + d
const g = c + e

// 取别名
const {a: a1} = obj
console.log(a1)  // 1

扩展运算符

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}

字符串模板

const name = '小明'
const score = 59
const result = `${name}的考试成绩${score > 60 ? '' : '不'}及格`

包含 includes

const condition = [1, 2, 3, 4]
if(condition.includes(type)) {
   //...
}

搜索、过滤 find

const a = [1, 2, 3, 4, 5]
const result = a.find( 
  item =>{
    return item === 3
  }
)

扁平化数组 flat

const deps = {
    '采购部': [1,2,3],
    '人事部': [5,8,12],
    '行政部': [5,14,79],
    '运输部': [3,64,105]
}
let member = Object.values(deps).flat(Infinity)
// 其中使用Infinity作为flat的参数,使得无需知道被扁平化的数组的维度

可选链操作符

const name = obj?.name
等价于
const name = obj && obj.name

空值合并运算符

if((value??'') !== '') {
  //...
}
等价于
if(value !== null && value !== undefined && value !== ''){
    //...
}
上一篇:TBBT.10.5


下一篇:结对项目(JAVA)