译者按: 又过了1年…原文:What’s New in JavaScript for 2019译者: Fundebug为了保证可读性,本文采用意译而非直译。另外,本文版权归原作者所有,翻译仅用于学习。最近这些年,ECMASCript标准发展节奏非常稳定,每年都会发布新的特性。那么,ECMASCript 2019可能会有哪些特性呢?ECMASCript语法提案的批准流程JavaScript的标准即为ECMAScript,其标准委员会是TC39。所有语法提案都需要经历标准的批准流程,该流程包括5个阶段:Stage 0 - Strawman(展示阶段)Stage 1 - Proposal(征求意见阶段)Stage 2 - Draft(草案阶段)Stage 3 - Candidate(候选阶段)Stage 4 - Finished(定案阶段)只有语法特性到达Stage 4,该特性才能成为正式的ECMAScript标准。但是,JS引擎例如V8(Chrome和Node.js)以及SpiderMonkey(Firefox)会试验性地支持Stage 4之前的语法特性,这样开发者可以进行测试和反馈。当我写这篇博客的时候,还没有新的Stage 4的语法提案,处于Stage 3的语法提案有好几个。ES2019可能不会包括所有Stage 3的语法提案。事实上,有些提案已经被搁置很多年了。Class相关变化有好几个提案是针对Class的,包括:属性定义私有方法及属性静态方法及属性代码示例如下:class Truck extends Automobile { model = “Heavy Duty”; // 公有属性 #numberOfSeats = 5; // 私有属性 #isCrewCab = true; static #name = “Truck”; // 静态私有属性 // 静态方法 static formattedName() { return This vehicle is a ${ Truck.#name }.
; } constructor( model, seats = 2 ) { super(); this.seats = seats; } // 私有方法 #getBodyType() { return this.#isCrewCab ? “Crew Cab” : “Standard Cab”; } bodyType() { return ${ this.#numberOfSeats }-passenger ${ this.model } ${ this.#getBodyType() }
; } get seats() { return this.#numberOfSeats; } set seats( value ) { if ( value >= 1 && value < 7 ) { this.#numberOfSeats = value; this.#isCrewCab = value > 3; } }}个人认为,使用#来定义私有成员不是很好,学习其他语言,使用private来定义显然更好。String的trimStart()与trimEnd()方法String有一个trim()方法可以移除字符串开头和结尾的空格,而trimStart()与trimEnd()方法则可以分别移除开头和结尾的空格:const one = " hello and let “;const two = “us begin. “;console.log( one.trimStart() + two.trimEnd() ) // 打印"hello and let us begin.“有趣的是,不少浏览器已经支持了这2个方法。可见,浏览器们一直在推动ECMASCript标准的进步。使用BigInt定义大整数Number所能定义的最大整数为2^53 ,对于更大数,则可以使用BigInt来定义:// 最大的Numberconst theBiggestIntegerToday = Number.MAX_SAFE_INTEGER; // 9007199254740991// 在整数后面添加n来定义BigIntconst ABiggerInteger = 9100000000000001n;// 使用BigInt()const EvenBigger = BigInt( 9100000000000002 ); // 9100000000000002n// 使用BigInt()const SuchBigWow = BigInt( “9100000000000003” ); // 9100000000000003n关于BigInt的更多使用示例,可以查看BigInt: arbitrary-precision integers in JavaScriptArray的flat()与flatMap()方法如果你学习过函数式编程,那么你应该知道flat()和flatMap()。flat()可以将一个包含嵌套数组的数组变换为一维数组。const nestedArraysOhMy = [ “a”, [“b”, “c”], [“d”, [“e”, “f”]]];// flat()的参数为数组的嵌套深度const ahhThatsBetter = nestedArraysOhMy.flat( 2 );console.log( ahhThatsBetter ); // [ “a”, “b”, “c”, “d”, “e”, “f” ]flatMap()与map()类似,当回调函数返回数组时,flatMap()返回的是一维数组,而map()返回的是嵌套数组:const scattered = [ “my favorite”, “hamburger”, “is a”, “chicken sandwich” ];const huh = scattered.map( chunk => chunk.split( " " ) );console.log( huh ); // [ [ “my”, “favorite” ], [ “hamburger” ], [ “is”, “a” ], [ “chicken”, “sandwich” ] ]const better = scattered.flatMap( chunk => chunk.split( " " ) );console.log( better ); // [ “my”, “favorite”, “hamburger”, “is”, “a”, “chicken”, “sandwich” ]其他ES2019候选特性这些是当前的Stage 3候选特性:globalThis动态import()遗留的RegExp特性 featuresimport.metaString的matchAll()方法Object.fromEntries()规范JSON.stringify标准化命令行程序的HashbangES2019什么时候发布?过去几年,TC39通常在6月份发布ECMAScript标准。因此,ES2019很可能也会在今年6月份发布。如何试用ES2019特性?其实有些特性其实JS引擎已经支持了,我们只需要配置一下就好了。使用Node.js 11Node.js使用V8引擎,而V8引擎已经支持了一些最新的特性,例如Array.prototype.flat和String.prototype.trimEnd,因此使用最新版的Node.js,即Node.js 11即可试用这些特性。我使用的Node.js版本为11.8.0:node -vv11.8.0如果要启用某个特性,可以使用node命令的–harmony-{feature-flag}选项。使用–v8-options,则可以查看node命令的所有选项,一些实验性的特性被标记为"in progress”。macOS / Linuxnode –v8-options | grep “in progress” –harmony-do-expressions (enable “harmony do-expressions” (in progress)) –harmony-class-fields (enable “harmony fields in class literals” (in progress)) –harmony-static-fields (enable “harmony static fields in class literals” (in progress)) –harmony-await-optimization (enable “harmony await taking 1 tick” (in progress)) –harmony-locale (enable “Intl.Locale” (in progress)) –harmony-intl-list-format (enable “Intl.ListFormat” (in progress)) –harmony-intl-relative-time-format (enable “Intl.RelativeTimeFormat” (in progress))Windowsnode –v8-options | find “in progress"例如,当我们的Node.js代码index.js中的Class有静态方法,则在执行的时候添加–harmony-static-fields选项即可:node –harmony-class-fields –harmony-static-fields index.js使用Babel 7.0 +使用Babel,我们就可以使用最新的JavaScript语法了,因为它会对代码进行转换以兼容旧的浏览器。Babel支持通过一些插件来支持实验性的JS特性。Babel所支持的ECMAScript特性提案可以查看babel/proposals仓库。参考Learn JavaScript in 2019!The History (and Future) of Asynchronous JavaScriptBuild a Secure Node.js Application with JavaScript Async Await Using HapiUse TypeScript to Build a Node API with ExpressStandard ECMA-262关于FundebugFundebug专注于JavaScript、微信小程序、微信小游戏、支付宝小程序、React Native、Node.js和Java线上应用实时BUG监控。 自从2016年双十一正式上线,Fundebug累计处理了9亿+错误事件,付费客户有Google、360、金山软件、百姓网等众多品牌企业。欢迎大家免费试用!版权声明转载时请注明作者Fundebug以及本文地址:https://blog.fundebug.com/2019/01/30/what-is-new-in-javascript-for-2019/