JavaScript ES2019中的8個新特性詳解
前言
JavaScript 不斷改進(jìn)和添加更多功能。TC39 已經(jīng)完成并批準(zhǔn)了 ES2019 的這 8 個功能,它有 4 個階段,這些階段是:
- Stage 0: Strawman
- Stage 1: Proposals
- Stage 2: Drafts
- Stage 3: Candidates
- Stage 4: Finished/Approved
以下鏈接可以查看Stage 0,Stage 1 – 3 和Final Stage
可選的 Catch 綁定
能夠在不使用 catch 綁定的地方選擇性地刪除它
try { // trying to use a new ES2019 feature // which may not be implemented in other browsers } catch (unused) { // revert back to old way }
現(xiàn)在可以刪除未使用的綁定
try { ... } catch { ... }
JSON 超集
此提議的動機(jī)是 JSON 字符串可以包含未轉(zhuǎn)義的 U + 2028 LINE SEPARATOR 和 U + 2029 PARAGRAPH SEPARATOR 字符,而 ECMAScript 字符串則不能。在 ES2019 之前,它會產(chǎn)生錯誤SyntaxError: Invalid or unexpected token
const LS = eval('"\u2028"'); const PS = eval("'\u2029'");
符號說明
在 ES2015 中引入符號,具有非常獨(dú)特的功能。在 ES2019 中,它現(xiàn)在可以提供給定的描述。其目的是避免間接獲得所提供的描述Symbol.prototype.toString
const mySymbol = Symbol("myDescription"); console.log(mySymbol); // Symbol(myDescription) console.log(mySymbol.toString()); // Symbol(myDescription) console.log(mySymbol.description); // myDescription
Function.prototype.toString - 修訂版
我們之前已經(jīng)在函數(shù)原型中使用了toString方法,但是在 ES2019 中它已被修改并包含函數(shù)內(nèi)的注釋,請注意它在Arrow Functions上不起作用。
function /* comment */ foo /* another comment */() {} // Before console.log(foo.toString()); // function foo(){} // Now ES2019 console.log(foo.toString()); // function /* comment */ foo /* another comment */ (){} // Arrow Syntax const bar /* comment */ = /* another comment */ () => {}; console.log(bar.toString()); // () => {}
Object.fromEntries
它是 Object.entries 的反向方法,它也是克隆對象的方法之一
const obj = { prop1: 1, prop2: 2 }; const entries = Object.entries(obj); console.log(entries); // [ [ 'prop1', 1 ], [ 'prop2', 2 ] ] const fromEntries = Object.fromEntries(entries); console.log(fromEntries); // Object { prop1: 1, prop2: 2 } console.log(obj === fromEntries); // false
注意:任何嵌入式對象/數(shù)組都只是通過引用復(fù)制。
格式良好的 JSON.stringify
這也是由同一個人提出的,并且與 JSON 超集特征有關(guān) 。ES2019 不是將未配對的代理代碼點(diǎn)作為單個 UTF-16 代碼單元返回,而是用 JSON 轉(zhuǎn)義序列表示它們
// Before console.log(JSON.stringify("\uD800")); // "�" // Now ES2019 console.log(JSON.stringify("\uD800")); // "\ud800"
String.prototype trimStart 和 trimEnd
我們已經(jīng)在 String 原型中使用了trim方法,它刪除了字符串開頭和結(jié)尾之間的空格。但是現(xiàn)在開始介紹 ES2019 的 trimStart和trimEnd
// Trim const name = " Codedam "; console.log(name.trim()); // "Codedam" // Trim Start const description = " Unlocks Secret Codes "; console.log(description.trimStart()); // "Unlocks Secret Codes " // Trim End const category = " JavaScript "; console.log(category.trimEnd()); // " JavaScript"
Array.prototype flat 和 flatMap
flat方法創(chuàng)建一個新數(shù)組,所有子數(shù)組元素以遞歸方式連接到指定的深度。 默認(rèn)情況下,深度為 1,使數(shù)組上第一層嵌套數(shù)組變平。
const arr = [1, 2, [3, 4, [5, 6]]]; arr.flat(); // [1, 2, 3, 4, [5, 6]] arr.flat(2); // [1, 2, 3, 4, 5, 6] // You can use Infinity to flatten all the nested arrays no matter how deep the array is const arrExtreme = [1, [2, [3, [4, [5, 6, 7, [8, 9]]]]]]; arrExtreme.flat(Infinity); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
flatMap 類似于 flat 并且與 map 相關(guān),其中它映射數(shù)組然后將其展平
const arr = ["Codedam", "is Awsome", "!"]; const mapResult = arr.map(item => item.split(" ")); console.log(mapResult); // [ [ 'Codedam' ], [ 'is', 'Awsome' ], [ '!' ] ] const flatMapResult = arr.flatMap(chunk => chunk.split(" ")); console.log(flatMapResult); // ['Codedam', 'is', 'Awsome', '!'];
其他
強(qiáng)調(diào)一下現(xiàn)在 Stage 3 中的一些有用的即將推出的功能。
- globalThis
- BigInt
- import()
- Legacy RegEx
- Private instance methods and accessors
- String.prototype.matchAll
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
比較詳細(xì)的關(guān)于javascript中void(0)的具體含義解釋
比較詳細(xì)的關(guān)于javascript中void(0)的具體含義解釋...2007-08-08javascript實(shí)現(xiàn)商品圖片放大鏡
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)商品圖片放大鏡,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11基于layui的table插件進(jìn)行復(fù)選框聯(lián)動功能的實(shí)現(xiàn)方法
今天小編就為大家分享一篇基于layui的table插件進(jìn)行復(fù)選框聯(lián)動功能的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09Javascript模塊化機(jī)制實(shí)現(xiàn)原理詳解
這篇文章主要介紹了Javascript模塊化機(jī)制實(shí)現(xiàn)原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04js 用CreateElement動態(tài)創(chuàng)建標(biāo)簽示例
用CreateElement動態(tài)創(chuàng)建標(biāo)簽,主要是html中常用的一些標(biāo)簽,在本文有詳細(xì)的示例,喜歡的朋友可以參考下2013-11-11JavaScript(JS) 壓縮 / 混淆 / 格式化 批處理工具
本工具所有的功能實(shí)現(xiàn)都是由 ttp://jscompress.sinaapp.com/api 處理.(包括現(xiàn)在可以使用的這個在線壓縮)2010-12-12webpack file-loader和url-loader的區(qū)別
這篇文章主要介紹了webpack file-loader和url-loader的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01