JavaScript ES2019中的8个新特性详解
前言
JavaScript不断改进和添加更多功能。TC39已经完成并批准了ES2019的这8个功能,它有4个阶段,这些阶段是:
- Stage0:Strawman
- Stage1:Proposals
- Stage2:Drafts
- Stage3:Candidates
- Stage4:Finished/Approved
以下链接可以查看Stage0,Stage1–3和FinalStage
可选的Catch绑定
能够在不使用catch绑定的地方选择性地删除它
try{ //tryingtouseanewES2019feature //whichmaynotbeimplementedinotherbrowsers }catch(unused){ //revertbacktooldway }
现在可以删除未使用的绑定
try{ ... }catch{ ... }
JSON超集
此提议的动机是JSON字符串可以包含未转义的U+2028LINESEPARATOR和U+2029PARAGRAPHSEPARATOR字符,而ECMAScript字符串则不能。在ES2019之前,它会产生错误SyntaxError:Invalidorunexpectedtoken
constLS=eval('"\u2028"'); constPS=eval("'\u2029'");
符号说明
在ES2015中引入符号,具有非常独特的功能。在ES2019中,它现在可以提供给定的描述。其目的是避免间接获得所提供的描述Symbol.prototype.toString
constmySymbol=Symbol("myDescription"); console.log(mySymbol);//Symbol(myDescription) console.log(mySymbol.toString());//Symbol(myDescription) console.log(mySymbol.description);//myDescription
Function.prototype.toString-修订版
我们之前已经在函数原型中使用了toString方法,但是在ES2019中它已被修改并包含函数内的注释,请注意它在ArrowFunctions上不起作用。
function/*comment*/foo/*anothercomment*/(){} //Before console.log(foo.toString());//functionfoo(){} //NowES2019 console.log(foo.toString());//function/*comment*/foo/*anothercomment*/(){} //ArrowSyntax constbar/*comment*/=/*anothercomment*/()=>{}; console.log(bar.toString());//()=>{}
Object.fromEntries
它是Object.entries的反向方法,它也是克隆对象的方法之一
constobj={ prop1:1, prop2:2 }; constentries=Object.entries(obj); console.log(entries);//[['prop1',1],['prop2',2]] constfromEntries=Object.fromEntries(entries); console.log(fromEntries);//Object{prop1:1,prop2:2} console.log(obj===fromEntries);//false
注意:任何嵌入式对象/数组都只是通过引用复制。
格式良好的JSON.stringify
这也是由同一个人提出的,并且与JSON超集特征有关。ES2019不是将未配对的代理代码点作为单个UTF-16代码单元返回,而是用JSON转义序列表示它们
//Before console.log(JSON.stringify("\uD800"));//"�" //NowES2019 console.log(JSON.stringify("\uD800"));//"\ud800"
String.prototypetrimStart和trimEnd
我们已经在String原型中使用了trim方法,它删除了字符串开头和结尾之间的空格。但是现在开始介绍ES2019的trimStart和trimEnd
//Trim constname="Codedam"; console.log(name.trim());//"Codedam" //TrimStart constdescription="UnlocksSecretCodes"; console.log(description.trimStart());//"UnlocksSecretCodes" //TrimEnd constcategory="JavaScript"; console.log(category.trimEnd());//"JavaScript"
Array.prototypeflat和flatMap
flat方法创建一个新数组,所有子数组元素以递归方式连接到指定的深度。默认情况下,深度为1,使数组上第一层嵌套数组变平。
constarr=[1,2,[3,4,[5,6]]]; arr.flat();//[1,2,3,4,[5,6]] arr.flat(2);//[1,2,3,4,5,6] //YoucanuseInfinitytoflattenallthenestedarraysnomatterhowdeepthearrayis constarrExtreme=[1,[2,[3,[4,[5,6,7,[8,9]]]]]]; arrExtreme.flat(Infinity);//[1,2,3,4,5,6,7,8,9]
flatMap类似于flat并且与map相关,其中它映射数组然后将其展平
constarr=["Codedam","isAwsome","!"]; constmapResult=arr.map(item=>item.split("")); console.log(mapResult);//[['Codedam'],['is','Awsome'],['!']] constflatMapResult=arr.flatMap(chunk=>chunk.split("")); console.log(flatMapResult);//['Codedam','is','Awsome','!'];
其他
强调一下现在Stage3中的一些有用的即将推出的功能。
- globalThis
- BigInt
- import()
- LegacyRegEx
- Privateinstancemethodsandaccessors
- String.prototype.matchAll
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。