注:仅学习记录medium 中的文章,详细了解请阅读原文 原文地址
1 交换变量 swap variables
let a = 'world', b = 'hello'
[a, b] = [b, a]
2 数组结构中使用 Async/Await
const [user, account] = await Promise.all([
fetch('/user'),
fetcg('/account')
])
3 调试
const a = 5, b = 6, c = 7
console.log({ a, b, c })
4 one liners
代码更加紧凑
//找到最大值
const max = (arr) => Math.max(...arr)
max([123,321,32]) //321
//求和
const sum = (arr) => arr.reduce((a,b) => (a+b),0)
sum([1,2,3,4]) //10
数组连接
代替原来的方法concat
const one = ['a', 'b', 'c']
const two = ['d', 'e', 'f']
const three = ['g', 'h', 'i']
//老的方法1
const result = one.concat(two, three)
//老得方法2
const result = [].concat(one, two, three)
//new
const result = [...one, ...two, ...three]
拷贝
数组和对象的拷贝:
const obj = { ...oldObj }
const arr = [ ...oldArr ]