分享ES6的7個(gè)實(shí)用技巧

Hack #1 交換元素

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供三都網(wǎng)站建設(shè)、三都做網(wǎng)站、三都網(wǎng)站設(shè)計(jì)、三都網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、三都企業(yè)網(wǎng)站模板建站服務(wù),10多年三都做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

利用 數(shù)組解構(gòu)來實(shí)現(xiàn)值的互換

let a = 'world', b = 'hello'
[a, b] = [b, a]
console.log(a) // -> hello
console.log(b) // -> world

Hack #2 調(diào)試

我們經(jīng)常使用 console.log()來進(jìn)行調(diào)試,試試 console.table()也無妨。

const a = 5, b = 6, c = 7
console.log({ a, b, c });
console.table({a, b, c, m: {name: 'xixi', age: 27}});

Hack #3 單條語句

ES6時(shí)代,操作數(shù)組的語句將會(huì)更加的緊湊

// 尋找數(shù)組中的最大值
const max = (arr) => Math.max(...arr);
max([123, 321, 32]) // outputs: 321
// 計(jì)算數(shù)組的總和
const sum = (arr) => arr.reduce((a, b) => (a + b), 0)
sum([1, 2, 3, 4]) // output: 10

Hack #4 數(shù)組拼接

展開運(yùn)算符可以取代 concat的地位了

const one = ['a', 'b', 'c']
const two = ['d', 'e', 'f']
const three = ['g', 'h', 'i']
const result = [...one, ...two, ...three]

Hack #5 制作副本

我們可以很容易的實(shí)現(xiàn)數(shù)組和對(duì)象的 淺拷貝

const obj = { ...oldObj }
const arr = [ ...oldArr ]

Hack #6 命名參數(shù)👍👍👍

解構(gòu)使得函數(shù)聲明和函數(shù)的調(diào)用更加可讀

// 我們嘗嘗使用的寫法
const getStuffNotBad = (id, force, verbose) => {
 ...do stuff
}
// 當(dāng)我們調(diào)用函數(shù)時(shí), 明天再看,尼瑪 150是啥,true是啥
getStuffNotBad(150, true, true)
// 看完本文你啥都可以忘記, 希望夠記住下面的就可以了
const getStuffAwesome = ({id, name, force, verbose}) => {
 ...do stuff
}
// 完美
getStuffAwesome({ id: 150, force: true, verbose: true })

Hack #7 Async/Await結(jié)合數(shù)組解構(gòu)

數(shù)組解構(gòu)非常贊!結(jié)合 Promise.all和 解構(gòu)和 await會(huì)使代碼變得更加的簡(jiǎn)潔

const [user, account] = await Promise.all([
 fetch('/user'),
 fetch('/account')
])

總結(jié)

以上所述是小編給大家介紹的分享ES6的7個(gè)實(shí)用技巧,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!

當(dāng)前標(biāo)題:分享ES6的7個(gè)實(shí)用技巧
轉(zhuǎn)載來源:http://bm7419.com/article46/jdepeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、動(dòng)態(tài)網(wǎng)站、用戶體驗(yàn)、服務(wù)器托管、網(wǎng)站策劃響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)