Home > Web Front-end > JS Tutorial > What are 7 practical tips about ES6?

What are 7 practical tips about ES6?

亚连
Release: 2018-06-11 15:27:52
Original
1313 people have browsed it

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
Copy after login

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}});
Copy after login

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
Copy after login

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]
Copy after login

Hack #5 Making a copy

We can easily implement arrays and a shallow copy of the object

const obj = { ...oldObj }
const arr = [ ...oldArr ]
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template