const a = 5, b = 6, c = 7 console.log({ a, b, c }) // outputs this nice object: // { // a: 5, // b: 6, // c: 7 // }
4. 封装
这么写可以显得代码更加紧凑
1 2 3 4 5 6
// Find max value constmax = (arr) => Math.max(...arr); max([123, 321, 32]) // outputs: 321 // Sum array constsum = (arr) => arr.reduce((a, b) => (a + b), 0) sum([1, 2, 3, 4]) // output: 10
5. 合并数组
可以使用展开运算符代替 concat
1 2 3 4 5 6 7 8 9
const one = ['a', 'b', 'c'] const two = ['d', 'e', 'f'] const three = ['g', 'h', 'i'] // Old way #1 const result = one.concat(two, three) // Old way #2 const result = [].concat(one, two, three) // New const result = [...one, ...two, ...three]