一文詳解如何在項(xiàng)目中和平時(shí)練習(xí)中應(yīng)用es6語法
一、關(guān)于取值
const obj = { a:1, b:2, c:3, d:4, e:5, } const a = obj.a; const b = obj.b; const c = obj.c; const d = obj.d; const e = obj.e;
es6解構(gòu)賦值:
const {a,b,c,d,e} = obj;
也可以使用:進(jìn)行重命名
const {a:a1} = obj; console.log(a1)
二、數(shù)組和對象的合并
const a = [1,2,3]; const b = [1,5,6]; const c = a.concat(b);//[1,2,3,1,5,6] const obj1 = { a:1, } const obj2 = { b:1, } const obj = Object.assign({}, obj1, obj2);//{a:1,b:1}
es6的擴(kuò)展運(yùn)算符,數(shù)組的合去重
const a = [1,2,3]; const b = [1,5,6]; const c = [...new Set([...a,...b])];//[1,2,3,5,6] const obj1 = { a:1, } const obj2 = { b:1, } const obj = {...obj1,...obj2};//{a:1,b:1}
三、字符串拼接
const name = '小明'; const score = 59; let result = ''; if(score > 60){ result = `${name}的考試成績及格`; }else{ result = `${name}的考試成績不及格`; }
es6模板字符串
在${}
中可以放入任意的JavaScript表達(dá)式,可以進(jìn)行運(yùn)算,以及引用對象屬性。
const name = '小明'; const score = 59; const result = `${name}${score > 60?'的考試成績及格':'的考試成績不及格'}`;
四、if條件多
if( type == 1 || type == 2 || type == 3 || type == 4 || ){ //... }
es6數(shù)組方法includes
const condition = [1,2,3,4]; if( condition.includes(type) ){ //... }
五、精確搜索
const a = [1,2,3,4,5]; const result = a.filter( item =>{ return item === 3 } )
es6數(shù)組find方法,性能優(yōu)化,find
方法中找到符合條件的項(xiàng),就不會(huì)繼續(xù)遍歷數(shù)組。
const a = [1,2,3,4,5]; const result = a.find( item =>{ return item === 3 } )
六、獲取對象屬性值
const name = obj && obj.name;
es6可選鏈?
const name = obj?.name;
可選鏈?補(bǔ)充:
會(huì)在嘗試訪問obj.name之前確定obj既不是null也不是undefined,如果obj是null或者undefined表達(dá)式會(huì)進(jìn)行短路計(jì)算直接返回undefined
七、輸入框非空判斷
if(value !== null && value !== undefined && value !== ''){ //... }
es6空位合并運(yùn)算符??
if((value??'') !== ''){ //... }
es6空位合并運(yùn)算符??補(bǔ)充:
前值是null或者undefined時(shí)表達(dá)式返回后值
const a = 0 ?? '默認(rèn)值a'; cosnt b = null ?? '默認(rèn)值b'; console.log(a); // "0" 0是假值,但不是 null 或者 undefined console.log(b); // "默認(rèn)值b"
拓展:
變量賦默認(rèn)值一般使用邏輯或操作符 || 。但是由于 ||
是一個(gè)布爾邏輯運(yùn)算符,左側(cè)的操作數(shù)會(huì)被強(qiáng)制轉(zhuǎn)換成布爾值用于求值。任何假值(0
, ''
, NaN
, null
, undefined
)都不會(huì)被返回。這導(dǎo)致如果你使用0
,''
或NaN
作為有效值,就會(huì)出現(xiàn)不可預(yù)料的后果。這也是 ?? 操作符可以解決這個(gè)問題
const a = 0; const b = a || 5 //當(dāng)0作為有效值,與我們期望的 b 的值不一致 console.log(b); // 5
八、異步函數(shù)回調(diào)
const fn1 = () =>{ return new Promise((resolve, reject) => { setTimeout(() => { resolve(1); }, 300); }); } const fn2 = () =>{ return new Promise((resolve, reject) => { setTimeout(() => { resolve(2); }, 600); }); } const fn = () =>{ fn1().then(res1 =>{ console.log(res1);// 1 fn2().then(res2 =>{ console.log(res2) }) }) }
es6的async和await
const fn = async () =>{ const res1 = await fn1(); const res2 = await fn2(); console.log(res1);// 1 console.log(res2);// 2 }
九、函數(shù)默認(rèn)參數(shù)
function fn (name, age) { let name = name || 'hsq' let age = age || 22 console.log(name, age) } fn() // hsq 22
es6函數(shù)默認(rèn)參數(shù)
function fn (name = 'hsq', age = 22) { console.log(name, age) } fn() // hsq 22 fn('test', 23) // test 23
十、剩余參數(shù)
一個(gè)函數(shù),傳入?yún)?shù)的個(gè)數(shù)是不確定的,這就可以用ES6的剩余參數(shù)
function fn (name, ...params) { console.log(name) console.log(params) } fn ('hsq', 1, 2) // hsq [ 1, 2 ] fn ('hsq', 1, 2, 3, 4, 5) // hsq [ 1, 2, 3, 4, 5 ]
十 一、箭頭函數(shù)
普通函數(shù)
function fn () {} const fn = function () {}
es6箭頭函數(shù)
const fn = () => {} // 如果只有一個(gè)參數(shù),可以省略括號(hào) const fn = name => {} // 如果函數(shù)體里只有一句return const fn = name => { return 2 * name } // 可簡寫為 const fn = name => 2 * name // 如果返回的是對象 const fn = name => ({ name: name })
普通函數(shù)和箭頭函數(shù)的區(qū)別:
- 1、箭頭函數(shù)不可作為構(gòu)造函數(shù),不能使用new
- 2、箭頭函數(shù)沒有原型對象
- 3、箭頭函數(shù)沒有自己的this
- 4、箭頭函數(shù)沒有arguments對象
十二、獲取對象的鍵值
Object.keys
可以用來獲取對象的key的集合
const obj = { name: 'hsq', age: 22, gender: '男' } const values = Object.keys(obj) console.log(values) // [ 'hsq', 22, '男' ]
Object.values
可以用來獲取對象的value的集合
const obj = { name: 'hsq', age: 22, gender: '男' } const values = Object.values(obj) console.log(values) // [ 'hsq', 22, '男' ]
Object.entries
可以用來獲取對象的鍵值對集合
const obj = { name: 'hsq', age: 22, gender: '男' } const entries = Object.entries(obj) console.log(entries) // [ [ 'name', 'hsq' ], [ 'age', 22 ], [ 'gender', '男' ] ]
十三、數(shù)組扁平化
Array.flat
有一個(gè)二維數(shù)組,我想讓他變成一維數(shù)組:
const arr = [1, 2, 3, [4, 5, 6]] console.log(arr.flat()) // [ 1, 2, 3, 4, 5, 6 ]
還可以傳參數(shù),參數(shù)為降維的次數(shù)
const arr = [1, 2, 3, [4, 5, 6, [7, 8, 9]]] console.log(arr.flat(2)) [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
如果傳的是一個(gè)無限大的數(shù)字,那么就實(shí)現(xiàn)了多維數(shù)組(無論幾維)降為一維數(shù)組
const arr = [1, 2, 3, [4, 5, 6, [7, 8, 9, [10, 11, 12]]]] console.log(arr.flat(Infinity)) [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]
總結(jié)
到此這篇關(guān)于如何在項(xiàng)目中和平時(shí)練習(xí)中應(yīng)用es6語法的文章就介紹到這了,更多相關(guān)項(xiàng)目應(yīng)用es6語法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
webpack圖片轉(zhuǎn)為base64的實(shí)現(xiàn)示例
在開發(fā)過程中,圖片轉(zhuǎn)成base64是常有的事,本文主要介紹了webpack圖片轉(zhuǎn)為base64的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12JavaScript動(dòng)態(tài)檢測密碼強(qiáng)度原理及實(shí)現(xiàn)方法詳解
這篇文章主要介紹了JavaScript動(dòng)態(tài)檢測密碼強(qiáng)度原理及實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式詳細(xì)分析了javascript針對輸入字符串密碼強(qiáng)度檢測的原理與相關(guān)判斷操作技巧,需要的朋友可以參考下2019-06-06JavaScript File API實(shí)現(xiàn)文件上傳預(yù)覽
這篇文章主要為大家介紹了JavaScript File API實(shí)現(xiàn)文件上傳預(yù)覽,F(xiàn)ile API將極大地方便 Web 端的文件上傳等操作,本文將介紹 File API的概況,并用兩個(gè)實(shí)例展示File API的應(yīng)用,感興趣的小伙伴們可以參考一下2016-02-02使用flutter創(chuàng)建可移動(dòng)的stack小部件功能
本文主要介紹我為桌面和 Web 設(shè)計(jì)的一個(gè)超級(jí)秘密 Flutter 項(xiàng)目使用了畫布和可拖動(dòng)節(jié)點(diǎn)界面。本教程將展示我如何使用堆棧來使用小部件完成可拖動(dòng)功能,需要的朋友可以參考下2021-10-10html+js實(shí)現(xiàn)簡單的計(jì)算器代碼(加減乘除)
下面小編就為大家?guī)硪黄猦tml+js實(shí)現(xiàn)簡單的計(jì)算器代碼(加減乘除)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07你所不了解的javascript操作DOM的細(xì)節(jié)知識(shí)點(diǎn)(一)
這篇文章主要介紹了你所不了解的javascript操作DOM的細(xì)節(jié)知識(shí)點(diǎn)的相關(guān)資料,需要的朋友可以參考下2015-06-06ionic進(jìn)入多級(jí)目錄后隱藏底部導(dǎo)航欄(tabs)的完美解決方案
這篇文章主要介紹了ionic進(jìn)入多級(jí)目錄后隱藏底部導(dǎo)航欄(tabs)的完美解決方案,在文章中用到了angularjs的指令知識(shí)點(diǎn),對ionic隱藏底部導(dǎo)航欄知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-11-11如何基于uni-app實(shí)現(xiàn)微信小程序一鍵登錄與退出登錄功能
uni-app 是使用vue的語法+小程序的標(biāo)簽和API的一套框架,是一套代碼多端使用,目前是大前端的一種趨勢,下面這篇文章主要給大家介紹了關(guān)于如何基于uni-app實(shí)現(xiàn)微信小程序一鍵登錄與退出登錄功能的相關(guān)資料,需要的朋友可以參考下2022-09-09JS倒計(jì)時(shí)兩種實(shí)現(xiàn)方式代碼實(shí)例
這篇文章主要介紹了JS倒計(jì)時(shí)兩種實(shí)現(xiàn)方式代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07