@@ -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-
4139Google公司的V8引擎已经部署了ES6的部分特性。使用node.js 0.11版,就可以体验这些特性。
4240
4341node.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+
7981npm install -g traceur
8082
81- # 运行ES6文件
83+ ```
84+
85+ 它有多种使用方法,首先是命令行工具。
86+
87+ ``` bash
88+
89+ # 运行ES6文件,在标准输出显示结果
8290traceur /path/to/es6
8391
8492# 将ES6文件转为ES5文件
8593traceur --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
901352013年3月,ES6的草案封闭,不再接受新功能了。新的功能将被加入ES7。
0 commit comments