Home > Web Front-end > JS Tutorial > 7 practical tips for ES6

7 practical tips for ES6

小云云
Release: 2018-01-19 09:14:42
Original
1359 people have browsed it

This article mainly shares with you 7 practical skills of es6. It is very good and has reference value. Friends who are interested can learn it together. I hope it can help everyone.

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, it doesn’t hurt to try console.table().


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 shallow copies of arrays and objects


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

Hack #6 Named Parameters

The above is the detailed content of 7 practical tips for 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