1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-24 18:32:22 +00:00

edit intro/traceur

This commit is contained in:
ruanyf 2014-05-03 19:20:43 +08:00
parent 4415a93eb3
commit 2b10934b80
2 changed files with 54 additions and 5 deletions

View File

@ -36,8 +36,6 @@ ECMA的第39号技术专家委员会Technical Committee 39简称TC39
由于ES6还没有定案有些语法规则还会变动目前支持ES6的软件和开发环境还不多。各大浏览器的最新版本对ES6的支持可以查看[kangax.github.io/es5-compat-table/es6/](http://kangax.github.io/es5-compat-table/es6/)。
## 使用方法
Google公司的V8引擎已经部署了ES6的部分特性。使用node.js 0.11版,就可以体验这些特性。
node.js的0.11版还不是稳定版本,要使用版本管理工具[nvm](https://github.com/creationix/nvm)切换。下载nvm以后进入项目目录运行下面的命令。
@ -72,19 +70,66 @@ $ node --v8-options | grep harmony
```
另外可以使用Google的[Traceur](https://github.com/google/traceur-compiler)将ES6代码编译为ES5。
## Traceur编译器
Google公司的[Traceur](https://github.com/google/traceur-compiler)编译器可以将ES6代码编译为ES5代码。
Traceur是一个node.js的模块需要用npm安装。
```bash
# 安装
npm install -g traceur
# 运行ES6文件
```
它有多种使用方法,首先是命令行工具。
```bash
# 运行ES6文件在标准输出显示结果
traceur /path/to/es6
# 将ES6文件转为ES5文件
traceur --script /path/to/es6 --out /path/to/es5
```
命令行下转换的文件就可以放到浏览器中运行。假定转换后的文件是result.js它插入浏览器的写法如下。
```html
<script src="bin/traceur-runtime.js"></script>
<script src="out/result.js"></script>
```
上面代码中的traceur-runtime.js是traceur提供的浏览器函数库。
Traceur的node.js用法如下。
```javascript
var traceur = require('traceur');
var fs = require('fs');
var contents = fs.readFileSync('es6-file.js').toString();
var result = traceur.compile(contents, {
filename: 'es6-file.js',
sourceMap: true,
// 其他设置
modules: 'commonjs'
});
if (result.error)
throw result.error;
fs.writeFileSync('out.js', result.js);
fs.writeFileSync('out.js.map', result.sourceMap);
```
除了命令行工具Traceur还提供一个[浏览器界面](http://google.github.io/traceur-compiler/demo/repl.html)用于在线转换ES6代码。
## ECMAScript 7
2013年3月ES6的草案封闭不再接受新功能了。新的功能将被加入ES7。

View File

@ -1,5 +1,8 @@
# 参考链接
- [ECMAScript 6 Language Specification](http://people.mozilla.org/~jorendorff/es6-draft.html): 语言规格草案
- [harmony:proposals](http://wiki.ecmascript.org/doku.php?id=harmony:proposals): ES6的各种提案
## 综合介绍
- Sayanee Basu, [Use ECMAScript 6 Today](http://net.tutsplus.com/articles/news/ecmascript-6-today/)
@ -33,5 +36,6 @@
## 工具
- Google, [traceur-compiler](https://github.com/google/traceur-compiler): Traceur编译器
- Casper Beyer, [ECMAScript 6 Features and Tools](http://caspervonb.github.io/2014/03/05/ecmascript6-features-and-tools.html)
- Stoyan Stefanov, [Writing ES6 today with jstransform](http://www.phpied.com/writing-es6-today-with-jstransform/)