Home >Web Front-end >JS Tutorial >What are 7 practical tips about ES6?
This article shares 7 practical skills of es6 with you. It is very good and has reference value. Friends who are interested can learn together
Hack #1 Exchange elements
Use array destructuring to achieve value exchange
let a = 'world', b = 'hello' [a, b] = [b, a] console.log(a) // -> hello console.log(b) // -> world
Hack #2 Debugging
We often use console.log() for debugging, try console .table() doesn’t hurt either.
const a = 5, b = 6, c = 7 console.log({ a, b, c }); console.table({a, b, c, m: {name: 'xixi', age: 27}});
Hack #3 Single statement
In the ES6 era, statements that operate on arrays will be more compact
// 寻找数组中的最大值 const max = (arr) => Math.max(...arr); max([123, 321, 32]) // outputs: 321 // 计算数组的总和 const sum = (arr) => arr.reduce((a, b) => (a + b), 0) sum([1, 2, 3, 4]) // output: 10
Hack #4 Array Splicing
The expansion operator can replace concat
const one = ['a', 'b', 'c'] const two = ['d', 'e', 'f'] const three = ['g', 'h', 'i'] const result = [...one, ...two, ...three]
Hack #5 Making a copy
We can easily implement arrays and a shallow copy of the object
const obj = { ...oldObj } const arr = [ ...oldArr ]
Hack #6 Named parameters
The above is the detailed content of What are 7 practical tips about ES6?. For more information, please follow other related articles on the PHP Chinese website!