最近这些年,ECMASCript标准发展节奏很是稳定,每一年都会发布新的特性。那么,ECMASCript 2019可能会有哪些特性呢?javascript
JavaScript的标准即为ECMAScript,其标准委员会是TC39。java
全部语法提案都须要经历标准的批准流程,该流程包括5个阶段:node
只有语法特性到达Stage 4,该特性才能成为正式的ECMAScript标准。可是,JS引擎例如V8(Chrome和Node.js)以及SpiderMonkey(Firefox)会试验性地支持Stage 4以前的语法特性,这样开发者能够进行测试和反馈。git
当我写这篇博客的时候,尚未新的Stage 4的语法提案,处于Stage 3的语法提案有好几个。ES2019可能不会包括全部Stage 3的语法提案。事实上,有些提案已经被搁置不少年了。github
有好几个提案是针对Class的,包括:web
代码示例以下:typescript
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
来定义显然更好。express
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标准的进步。小程序
Number所能定义的最大整数为2^53 ,对于更大数,则可使用BigInt来定义:
// 最大的Number const theBiggestIntegerToday = Number.MAX_SAFE_INTEGER; // 9007199254740991 // 在整数后面添加n来定义BigInt const ABiggerInteger = 9100000000000001n; // 使用BigInt() const EvenBigger = BigInt( 9100000000000002 ); // 9100000000000002n // 使用BigInt() const SuchBigWow = BigInt( "9100000000000003" ); // 9100000000000003n
关于BigInt的更多使用示例,能够查看BigInt: arbitrary-precision integers in JavaScript
若是你学习过函数式编程,那么你应该知道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" ]
这些是当前的Stage 3候选特性:
过去几年,TC39一般在6月份发布ECMAScript标准。所以,ES2019极可能也会在今年6月份发布。
其实有些特性其实JS引擎已经支持了,咱们只须要配置一下就行了。
Node.js使用V8引擎,而V8引擎已经支持了一些最新的特性,例如Array.prototype.flat和String.prototype.trimEnd,所以使用最新版的Node.js,即Node.js 11便可试用这些特性。
我使用的Node.js版本为11.8.0:
node -v v11.8.0
若是要启用某个特性,可使用node命令的--harmony-{feature-flag}
选项。使用--v8-options
,则能够查看node命令的全部选项,一些实验性的特性被标记为"in progress"。
macOS / Linux
node --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))
Windows
node --v8-options | find "in progress"
例如,当咱们的Node.js代码index.js中的Class有静态方法,则在执行的时候添加--harmony-static-fields
选项便可:
node --harmony-class-fields --harmony-static-fields index.js
使用Babel,咱们就可使用最新的JavaScript语法了,由于它会对代码进行转换以兼容旧的浏览器。
Babel支持经过一些插件来支持实验性的JS特性。Babel所支持的ECMAScript特性提案能够查看babel/proposals仓库。
Fundebug专一于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/