Skip to content

Commit 2b10934

Browse files
committed
edit intro/traceur
1 parent 4415a93 commit 2b10934

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

docs/intro.md

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ ECMA的第39号技术专家委员会(Technical Committee 39,简称TC39)负
3636

3737
由于ES6还没有定案,有些语法规则还会变动,目前支持ES6的软件和开发环境还不多。各大浏览器的最新版本,对ES6的支持可以查看[kangax.github.io/es5-compat-table/es6/](http://kangax.github.io/es5-compat-table/es6/)
3838

39-
## 使用方法
40-
4139
Google公司的V8引擎已经部署了ES6的部分特性。使用node.js 0.11版,就可以体验这些特性。
4240

4341
node.js的0.11版还不是稳定版本,要使用版本管理工具[nvm](https://github.com/creationix/nvm)切换。下载nvm以后,进入项目目录,运行下面的命令。
@@ -72,19 +70,66 @@ $ node --v8-options | grep harmony
7270

7371
```
7472

75-
另外,可以使用Google的[Traceur](https://github.com/google/traceur-compiler),将ES6代码编译为ES5。
73+
## Traceur编译器
74+
75+
Google公司的[Traceur](https://github.com/google/traceur-compiler)编译器,可以将ES6代码编译为ES5代码。
76+
77+
Traceur是一个node.js的模块,需要用npm安装。
7678

7779
```bash
78-
# 安装
80+
7981
npm install -g traceur
8082

81-
# 运行ES6文件
83+
```
84+
85+
它有多种使用方法,首先是命令行工具。
86+
87+
```bash
88+
89+
# 运行ES6文件,在标准输出显示结果
8290
traceur /path/to/es6
8391

8492
# 将ES6文件转为ES5文件
8593
traceur --script /path/to/es6 --out /path/to/es5
94+
8695
```
8796

97+
命令行下转换的文件,就可以放到浏览器中运行。假定转换后的文件是result.js,它插入浏览器的写法如下。
98+
99+
```html
100+
101+
<script src="bin/traceur-runtime.js"></script>
102+
<script src="out/result.js"></script>
103+
104+
```
105+
106+
上面代码中的traceur-runtime.js,是traceur提供的浏览器函数库。
107+
108+
Traceur的node.js用法如下。
109+
110+
```javascript
111+
112+
var traceur = require('traceur');
113+
var fs = require('fs');
114+
115+
var contents = fs.readFileSync('es6-file.js').toString();
116+
117+
var result = traceur.compile(contents, {
118+
filename: 'es6-file.js',
119+
sourceMap: true,
120+
// 其他设置
121+
modules: 'commonjs'
122+
});
123+
124+
if (result.error)
125+
throw result.error;
126+
fs.writeFileSync('out.js', result.js);
127+
fs.writeFileSync('out.js.map', result.sourceMap);
128+
129+
```
130+
131+
除了命令行工具,Traceur还提供一个[浏览器界面](http://google.github.io/traceur-compiler/demo/repl.html),用于在线转换ES6代码。
132+
88133
## ECMAScript 7
89134

90135
2013年3月,ES6的草案封闭,不再接受新功能了。新的功能将被加入ES7。

docs/reference.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# 参考链接
22

3+
- [ECMAScript 6 Language Specification](http://people.mozilla.org/~jorendorff/es6-draft.html): 语言规格草案
4+
- [harmony:proposals](http://wiki.ecmascript.org/doku.php?id=harmony:proposals): ES6的各种提案
5+
36
## 综合介绍
47

58
- Sayanee Basu, [Use ECMAScript 6 Today](http://net.tutsplus.com/articles/news/ecmascript-6-today/)
@@ -33,5 +36,6 @@
3336

3437
## 工具
3538

39+
- Google, [traceur-compiler](https://github.com/google/traceur-compiler): Traceur编译器
3640
- Casper Beyer, [ECMAScript 6 Features and Tools](http://caspervonb.github.io/2014/03/05/ecmascript6-features-and-tools.html)
3741
- Stoyan Stefanov, [Writing ES6 today with jstransform](http://www.phpied.com/writing-es6-today-with-jstransform/)

0 commit comments

Comments
 (0)