-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yibuyisheng
committed
Oct 18, 2015
1 parent
c215349
commit ff8a4ee
Showing
29 changed files
with
176 additions
and
49 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## 3.3 总结 | ||
|
||
一个 JavaScript 意思就是使 ECMAScript 6 完全地向后兼容,很高兴这获得了成功。尤其是模块隐式就是严格模式的(这样一来我们大部分的代码都会处于严格模式下)。 | ||
|
||
在短期内,对于制定 ES6 规范和引擎实现来说,给严格模式和松散模式添加 ES6 的语法结构会耗费更多的精力。从长远来看,规范和引擎将会受益于语言不分叉(更少的膨胀等等)。开发人员会立即从一个 JavaScript 中获得好处,因为开始使用 ECMAScript 6 变得更加容易。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 3.4 深入阅读 | ||
|
||
[1] 原始的 1 JS 提案(警告:已过时): “ [ES6 doesn’t need opt-in](http://esdiscuss.org/topic/es6-doesn-t-need-opt-in) ”,作者 David Herman 。 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
# 5 部署 ECMAScript 6 | ||
|
||
本章介绍了在当前 JavaScript 环境中部署 ECMAScript 6 的方法。目前有一系列工具可以选择,如果你想要一个这些工具的完整列表,我建议去看看 Addy Osmani 的“ [ECMAScript Tools](https://github.com/addyosmani/es6-tools) ”。 | ||
|
||
## 5.1 现在就使用 ECMAScript 6 | ||
|
||
现在使用 ECMAScript 6 有些什么方式? | ||
|
||
ECMAScript 6 的特性在引擎中逐渐被实现。你可以在 Kangax 的“ [ECMAScript 6 compatibility table](http://kangax.github.io/compat-table/es6/) ”中找到目前哪些特性已经被支持了。预计第一个完整支持 ECMAScript 6 的引擎将会在2015年底或2016年初出现,所有引擎的支持会等更长的时间。 | ||
|
||
尤其是如果你要考虑老的引擎,将 ES6 编译成 ES5 是唯一切实可行的使用 ES6 的方式。把一种源码编译成另一种源码也称为*转换( transpiling )*。你可以在部署之前转换 ES6 代码(静态地),也可以在运行时转换(动态地)。下一部分讲解了其中的运行原理,再后面的部分介绍了其他 ES6 工具和库。 | ||
|
||
ES6 有一个好处就是,它是 ES5 的超集,也就是说,所有 ES5 代码都是合法的 ES6 代码。这就使得接受 ES6 特有的特性变得很轻松,因为可以逐渐开始使用 ES6 的特性。 | ||
|
||
### 5.1.1 原生地使用 ECMAScript 6 | ||
|
||
在第一个完全支持 ES6 的引擎出现之后,所有不完全支持或不支持 ES6 的引擎消失之前,客户端应用中就会存在一种混合的方式: | ||
|
||
* 在服务器端,每个源码文件都有两个版本:原生的 ES6 版本和转换后的版本( ES5 版本)。 | ||
* web 应用启动的时候,使用特性检测来判断当前引擎是否完全支持 ES6 。如果完全支持,则使用应用的 ES6 版本代码;否则,使用 ES5 版本代码。 | ||
|
||
检测 ECMAScript 版本很难,因为很多引擎在完全支持所有特性之前只是部分支持。例如,下面就是如何去检测某个引擎是否支持 ECMAScript 6 的 `for-of` 循环: | ||
|
||
```js | ||
function isForOfSupported() { | ||
try { | ||
eval("for (var e of ['a']) {}"); | ||
return true; | ||
} catch (e) { | ||
// Possibly: check if e instanceof SyntaxError | ||
} | ||
return false; | ||
} | ||
``` | ||
|
||
Kyle Simpson 的库 [ES Feature Tests](https://github.com/getify/es-feature-tests) 能够检测某个引擎是否支持 ES6 : | ||
|
||
```js | ||
var ReflectSupports = require("es-feature-tests"); | ||
|
||
ReflectSupports("all", function (results) { | ||
if (results.letConst && results.arrow) { | ||
// Use ES6 natively | ||
} else { | ||
// Use transpiled ES6 | ||
} | ||
}); | ||
``` | ||
|
||
npm 可能最后会支持某一个模块的两个版本,这样就能够为 Node.js 、 io.js 和基于 npm 的客户端模块系统提供 ES5 和 ES6 版本的库。 | ||
|
||
## 5.2 转换工具 | ||
|
||
对于转换,必须要做三个选择: | ||
|
||
* 转换器(用于转换源码) | ||
* 包管理器(用于安装已有的库) | ||
* 模块系统(用于整个应用) | ||
|
||
注意,这三个选择并不完全独立,比如不是每个模块系统适用于每个包管理器。下一部分更加详细地介绍了每一个选择。 | ||
|
||
### 5.2.1 选择转换器 | ||
|
||
转换器将你的 ES6 代码转换成 ES5 代码。流行的转换器有: | ||
|
||
* [TypeScript](http://www.typescriptlang.org/) : 转换 ES6 代码和可选的类型注解。 | ||
* [Traceur](https://github.com/google/traceur-compiler) : Google 开发的转换器,是第一个流行的转换工具。 | ||
* [Babel](https://babeljs.io/) : 是一个新的 ES6 转换器,最近它的流行程度大大提升。除了 ES6 之外, Babel 还支持 React 的 JSX 语法。 | ||
|
||
你可以在如下的阶段转换源码: | ||
|
||
* 在部署之前(静态地) | ||
* 在运行时(动态地) | ||
|
||
#### 5.2.1.1 静态转换 | ||
|
||
作为构建的其中一步, TypeScript , Traceur 和 Babel 能生成如下模块格式的 ES5 代码。你可以直接调用它们,或者使用构建工具( grunt , gulp , broccoli ,等等)。 | ||
|
||
* AMD | ||
* CommonJS | ||
* ES6 模块加载 API : 被转换成 ES5 代码的 ES6 代码通过一个 [polyfill](https://github.com/ModuleLoader/es6-module-loader) 来使用这个 API 。 TypeScript 不支持这种模块格式。 | ||
|
||
在浏览器中,通过后面介绍的模块系统之一来加载这种 ES5 代码。在 Node.js 中,可以使用内置的模块系统(存在其他方案,比如 webpack 和 ES6 模块加载 Polyfill )。 | ||
|
||
#### 5.2.1.2 动态转换 | ||
|
||
在浏览器中,用 `<script>` 标签引入额外的转换库来动态转换 ES6 代码。 [Traceur](https://github.com/google/traceur-compiler/wiki/Getting-Started) 和 [Babel](https://babeljs.io/docs/usage/browser/) 支持这种方式。 | ||
|
||
对于 Node.js , Babel 提供了动态转换的工具,在另一部分中介绍了该工具。 | ||
|
||
### 5.2.2 选择一个包管理器 | ||
|
||
需要一个包管理器来安装第三方的库。有三个流行的包管理器: | ||
|
||
* [npm](https://www.npmjs.com/) ( CommomJS 模块):是一个专为 Node.js 创建的包管理器,但是在客户端开发也逐渐流行起来了,这要归功于模块打包工具和加载工具(比如 browserify 和 webpack )。 | ||
* [Bower](http://bower.io/) ( CommonJS 或者 AMD 模块):是一个客户端代码的包管理器。 | ||
* [jspm](http://jspm.io/) : 是一个为 SystemJS 打造的包管理器(参考下一个清单)。它可以从大量的源中安装模块,包括 GitHub 和 npm 。 jspm 的一个关键特性就是外部的模块也可以是 ES6 代码(会被转换),而不仅仅是你自己的模块。 | ||
|
||
### 5.2.3 选择模块系统 | ||
|
||
模块系统对 ES5 浏览器有支持( Node.js 有内置的模块系统)。这样一来,你就可以基于模块来构建应用了。流行的模块系统有: | ||
|
||
* [RequireJS](http://requirejs.org/) :是一个 AMD 模块加载器,模块可以通过 TypeScript 、 Traceur 或者 Babel 静态创建。*加载器插件*(基于 Traceur 和 Babel )使其能够加载 ES6 模块。 | ||
* [Browserify](http://browserify.org/) :打包 CommonJS 模块(包括通过 npm 安装的模块)以便能在浏览器中加载。可以和 Traceur 或 Babel 一起使用,从而支持 ES6 模块。 | ||
* [webpack](http://webpack.github.io/) :一个支持 CommomJS 模块(包括通过 npm 安装的模块)和 AMD 模块(包括通过 Bower 打包的模块)的打包器和加载器。可以通过基于 Traceur 和 Babel 的*自定义加载器*(插件)来支持 ES6 模块。 | ||
* [SystemJS](https://github.com/systemjs/systemjs) :基于 ES6 模块加载器 Polyfill 的模块系统,支持以 CommomJS 、 AMD 和“ ES6 模块加载器 API ”格式书写的 ES6 模块和 ES5 模块。 | ||
|
||
## 5.3 其他有用的 ES6 工具和库 | ||
|
||
* 测试工具(比如 Jasmine 和 mocha ) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters