🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] # 1. 关于 ECMAScript 6 (ES6) 虽然花了很长时间才完成,但是 ECMAScript 6(下一个版本的JavaScript)规范,终于标准化了: - 它在[2015年6月17日成为了标准](http://www.ecma-international.org/news/Publication%20of%20ECMA-262%206th%20edition.htm). - 它的大多数特性已经广泛可用(如 kangax 的 [ES6兼容性表](http://kangax.github.io/compat-table/es6/))。 - 转译器(Transpilers)(如 [Babel](https://babeljs.io/))可以将 ES6 编译为 ES5语法。 下一节将解释在 ES6 世界中重要的概念。 ## 1.1 TC39 (Ecma技术委员会 39) [TC39 (Ecma Technical Committee 39)](http://www.ecma-international.org/memento/TC39.htm)是发展JavaScript的委员会。Its members are companies (among others, all major browser vendors). [TC39 meets regularly](http://www.ecma-international.org/memento/TC39-M.htm), its meetings are attended by delegates that members send and by invited experts. Minutes of the meetings are [available online](https://github.com/tc39/tc39-notes) and give you a good idea of how TC39 works. ## 1.2 ECMAScript 6是如何设计的 The ECMAScript 6 design process centers on *proposals* for features. Proposals are often triggered by suggestions from the developer community. To avoid design by committee, proposals are maintained by *champions* (1–2 committee delegates). A proposal goes through the following steps before it becomes a standard: - Sketch (informally: “strawman proposal”): A first description of the proposed feature. - Proposal: If TC39 agrees that a feature is important, it gets promoted to official proposal status. That does not guarantee it will become a standard, but it considerably increases its chances. The deadline for ES6 proposals was May 2011. No major new proposals were considered after that. - Implementations: Proposed features must be implemented. Ideally in two JavaScript engines. Implementations and feedback from the community shape the proposal as it evolves. - Standard: If the proposal continues to prove itself and is accepted by TC39, it will eventually be included in an edition of the ECMAScript standard. At this point, it is a standard feature. \[Source of this section: “[The Harmony Process](http://tc39wiki.calculist.org/about/harmony/)” by David Herman.\] ### 1.2.1 在ES6之后的设计过程 Starting with ECMAScript 2016 (ES7), TC39 will time-box releases. A new version of ECMAScript will be released every year, with whatever features are ready at that time. That means that from now on, ECMAScript versions will be relatively small upgrades. For more information on the new process, including finished and upcoming feature proposals, consult [the GitHub repository `ecma262`](https://github.com/tc39/ecma262). ## 1.3 JavaScript 对比 ECMAScript JavaScript is what everyone calls the language, but that name is trademarked (by Oracle, which inherited the trademark from Sun). Therefore, the official name of JavaScript is *ECMAScript*. That name comes from the standards organization Ecma, which manages the language standard. Since ECMAScript’s inception, the name of the organization has changed from the acronym “ECMA” to the proper name “Ecma”. Versions of JavaScript are defined by specifications that carry the official name of the language. Hence, the first standard version of JavaScript is ECMAScript 1 which is short for “ECMAScript Language Specification, Edition 1”. ECMAScript x is often abbreviated ESx. ## 1.4 升级到ES6 The stake holders on the web are: - Implementors of JavaScript engines - Developers of web applications - Users These groups have remarkably little control over each other. That’s why upgrading a web language is so challenging. On one hand, upgrading engines is challenging, because they are confronted with all kinds of code on the web, some of which is very old. You also want engine upgrades to be automatic and unnoticeable for users. Therefore, ES6 is a superset of ES5, nothing is removed[1](leanpub-endnotes.html#fn-introduction_1). ES6 upgrades the language without introducing versions or modes. It even manages to make strict mode the de-facto default (via modules), without increasing the rift between it and sloppy mode. The approach that was taken is called “One JavaScript” and explained in [a separate chapter](ch_one-javascript.html#ch_one-javascript). On the other hand, upgrading code is challenging, because your code must run on all JavaScript engines that are used by your target audience. Therefore, if you want to use ES6 in your code, you only have two choices: You can either wait until no one in your target audience uses a non-ES6 engine, anymore. That will take years; mainstream audiences were at that point w.r.t. ES5 when ES6 became a standard in June 2015. And ES5 was standardized in December 2009! Or you can compile ES6 to ES5 and use it now. More information on how to do that is given in the book “[Setting up ES6](https://leanpub.com/setting-up-es6)”, which is free to read online. Goals and requirements clash in the design of ES6: - Goals are fixing JavaScript’s pitfalls and adding new features. - Requirements are that both need to be done without breaking existing code and without changing the lightweight nature of the language. ## 1.5 Goals for ES6 [The original project page for Harmony/ES6](http://wiki.ecmascript.org/doku.php?id=harmony:harmony) mentions several goals. In the following subsections, I’m taking a look at some of them. ### 1.5.1 目标:成为更好的语言 The goal is: Be a better language for writing: 1. complex applications; 2. libraries (possibly including the DOM) shared by those applications; 3. code generators targeting the new edition. Sub-goal (i) acknowledges that applications written in JavaScript have grown huge. A key ES6 feature fulfilling this goal is built-in modules. Modules are also an answer to goal (ii). As an aside, the DOM is notoriously difficult to implement in JavaScript. [ES6 Proxies](ch_proxies.html#ch_proxies) should help here. Several features were mainly added to make it easier to compile to JavaScript. Two examples are: - `Math.fround()` – rounding Numbers to 32 bit floats - `Math.imul()` – multiplying two 32 bit ints They are both useful for, e.g., compiling C/C++ to JavaScript via [Emscripten](https://github.com/kripken/emscripten). ### 1.5.2 目标:提高交互性 The goal is: Improve interoperation, adopting de facto standards where possible. Examples are: - Classes: are based on how constructor functions are currently used. - Modules: picked up design ideas from the CommonJS module format. - Arrow functions: have syntax that is borrowed from CoffeeScript. - Named function parameters: There is no built-in support for named parameters. Instead, the existing practice of naming parameters via object literals is supported via [destructuring in parameter definitions](ch_parameter-handling.html#sec_named-parameters). ### 1.5.3 目标:版本控制 The goal is: Keep versioning as simple and linear as possible. As mentioned previously, ES6 avoids versioning via “[One JavaScript](ch_one-javascript.html#ch_one-javascript)”: In an ES6 code base, everything is ES6, there are no parts that are ES5-specific. ## 1.6 ES6 特性的类别 The introduction of the ES6 specification lists all new features: > Some of \[ECMAScript 6’s\] major enhancements include modules, class declarations, lexical block scoping, iterators and generators, promises for asynchronous programming, destructuring patterns, and proper tail calls. The ECMAScript library of built-ins has been expanded to support additional data abstractions including maps, sets, and arrays of binary numeric values as well as additional support for Unicode supplemental characters in strings and regular expressions. The built-ins are now extensible via subclassing. 在特性上主要有三大类别: - Better syntax for features that already exist (e.g. via libraries). For example: - [Classes](ch_classes.html#ch_classes) - [Modules](ch_modules.html#ch_modules) - New functionality in the standard library. For example: - New methods for [strings](ch_strings.html#ch_strings) and [Arrays](ch_arrays.html#ch_arrays) - [Promises](ch_promises.html#ch_promises) - [Maps, Sets](ch_maps-sets.html#ch_maps-sets) - Completely new features. For example: - [Generators](ch_generators.html#ch_generators) - [Proxies](ch_proxies.html#ch_proxies) - [WeakMaps](ch_maps-sets.html#sec_weakmap) ## 1.7 ECMAScript简史 本节描述在ECMAScript 6 发展史。 ### 1.7.1 早期:ECMAScript 1-3 - **ECMAScript 1 (June 1997)** 是JavaScript语言标准的第一个版本。 - **ECMAScript 2 (June 1998)** 包含了一些小的变化,以保持规范与单独的 JavaScript ISO 标准同步。 - **ECMAScript 3 (December 1999)** 引入了许多已经成为该语言流行部分的特性,如 ES6 规范的介绍中所述:“`[…]`、正则表达式、更好的字符串处理、新的控制语句、`try/catch`异常处理、更严格的错误定义、数值输出的格式化和其他增强。” ### 1.7.2 ECMAScript 4 (2008年7月被遗弃) 在 1999 年 ES3 发布之后,就开始了 ES4 的制定工作。在 2003 年,发布了一份临时报告,在这之后 ES4 的制定工作就暂停了。 Adobe 的 ActionScript 和微软的 JScript.NET 实现了临时报告中所描述语言的子级。 在 2005 年 2 月, Jesse James Garrett 发现新技术变得流行起来,被用于实现动态的使用 JavaScript 的前端 app 。[他称呼这些技术为 Ajax](http://www.adaptivepath.com/ideas/ajax-new-approach-web-applications/)。 Ajax 开启了 web app 的全新世界,并使人们对 JavaScript 产生了浓厚的兴趣。 这使得 TC39 在 2005 年秋天重启 ES4 的制定工作。他们基于 ES3 来制定 ES4 ,临时的 ES4 在 ActionScript 和 JScript.NET 中实现。 当时有两个组从事制定未来 ECMAScript 版本的工作: * ECMAScript 4 由 Adobe , Mozilla , Opera 和 Google 设计,是一个很大的升级。它包含的特性集如下: * 大型编程(类,接口,命名空间,包,程序单元,可选的类型注释,可选的静态类型检测和验证) * 编程和脚本的进化(结构类型,鸭子类型,类型定义,和方法重载) * 数据结构完善(参数化类型, getter 和 setter ,和元级的方法) * 控制抽象(恰当的尾递归,迭代器,和生成器) * 反思(类型元对象和堆栈标记) * ECMAScript 3.1 由微软和 Yahoo 设计。计划成为 ES4 的子集,并且是 ECMAScript 3 的增量化升级,没有 bug 修复和小的新特性。 ECMAScript 3.1 最终成为了 ECMAScript 5 。 这两个组关于 JavaScript 未来产生了分歧,他们之间的紧张局势也逐渐升级。 > 本节来源: > - [《 Proposed ECMAScript 4th Edition – Language Overview 》](http://www.ecmascript.org/es4/spec/overview.pdf),2007-10-23 > - [《 ECMAScript Harmony 》](http://ejohn.org/blog/ecmascript-harmony/),作者 John Resig ,2008-08-13 ### 1.7.3 ECMAScript Harmony 2008 年 7 月底,TC39 在 Oslo 开了个会, Brendan Eich[描述](https://mail.mozilla.org/pipermail/es-discuss/2008-August/006837.html)会议结果如下: > JavaScript 标准正文不是秘密, Ecma 的技术委员会 39 已经分裂超过一年了,一些成员喜欢 ES4 [...] ,一些成员倡导 ES3.1 [...] 。现在,我很高兴地说,分裂结束了。 会议就四点上达成了一致: * 1、开发一个增量升级的 ECMAScript (就是 ECMAScript 5 )。 * 2、开发一个主要的新版本,相对于 ES4 更加现代化,但是在范围上比 ECMAScript 3 之后的版本大得多。这个版本代号为*Harmony*,名字源于会议要达成的消除分歧的目标。 * 3、来自于 ECMAScript 4 的特性被取消了一部分:包,命名空间,早绑定。 * 4、其他想法在 TC39 达成一致后才开发。 因此: ES4 小组同意不让 Harmony 像 ES4 那么激进,剩下的 TC39 成员赞成继续推进。 接下来的 ECMAScript 版本是:接下来的 ECMAScript 版本是: * ECMAScript 5 (2009 年 12 月)。这是目前大多数浏览器实现的 ECMAScript 版本。它给标准库引入了几处增强,并且通过*严格模式*升级了语言的语义。 * ECMAScript 6 (2015 年 6 月)。该版本经历了几次名字修改: * ECMAScript Harmony : 是最初的代号,用于 ECMAScript 5 之后的 JavaScript 的改进。 * ECMAScript.next : 很明显, Harmony 的计划对于一次版本升级来说显得太雄心勃勃了,因此它的特性分成了两组:第一组有很高的优先级,成为继 ES5 之后的版本。为了避免过早地涉及到版本号,这个版本的代号就是 ECMAScript.next , ES4 就因为这个产生了很多问题。第二组特性直到 ECMAScript.next 之后才有时间了。 * ECMAScript 6 : 当 ECMAScript.next 成熟之后,它的代号就弃用了,大家都开始称其为 ECMAScript 6 。 * ECMAScript 2015 : 在 2014 年底, TC39 决定将官方的 ECMAScript 6 名字修改为 ECMAScript 2015 ,根据接下来的每年发布规范的策略。 * ECMAScript 2016 之前被叫做 ECMAScript 7 。开始于 ES2016 ,语言标准将会每年发布一个小的版本。