简体   繁体   English

我现在如何编写和测试ECMAScript 6代码?

[英]How can I write and test ECMAScript 6 code now?

I would like to start writing code using the up and coming ECMAScript 6 (ES6) so as to start getting to grips with the new syntax. 我想开始使用即将推出的ECMAScript 6(ES6)编写代码,以便开始掌握新语法。

Is there some kind of web resource or browser plugin that I can use to play (write and test code) on what we currently have in regards to ES6? 是否有某种网络资源或浏览器插件,我可以用来播放(编写和测试代码)我们目前有关ES6的内容?

I was lead to believe that using Google Chrome Canary might be able to help. 我一直认为使用Google Chrome Canary可能会有所帮助。 So I downloaded Canary, I enabled a couple of features in Canary: 所以我下载了Canary,我启用了Canary的一些功能:

Enable Experimental JavaScript (Mac, Windows, Linux, Chrome OS, Android) 启用实验性JavaScript (Mac,Windows,Linux,Chrome OS,Android)

Enable web pages to use experimental JavaScript features. 启用网页以使用实验性JavaScript功能。
#enable-javascript-harmony Enable #enable-javascript-harmony 启用

And after testing the let block scope in a for loop 并在for循环中测试let块作用域之后

for (let i = 0; i < 10; i++) {
   console.log(i);
}

I got a syntax error: 我遇到了语法错误:

SyntaxError: Unexpected identifier SyntaxError:意外的标识符

The following works in Chrome 37 (current Chrome) with the Experimental JavaScript flag enabled: 以下内容适用于Chrome 37(当前Chrome)并启用了实验性JavaScript标记:

(function () {
   "use strict"; 
   for (let i = 0; i < 10; i++) {
      console.log(i);
   }
})()

Outside strict mode, you should see SyntaxError: Illegal let declaration outside extended mode or SyntaxError: Unexpected identifier if you're not in strict mode, or possibly SyntaxError: Unexpected strict mode reserved word if the Experimental JavaScript flag is not enabled. 在严格模式之外,您应该看到SyntaxError: Illegal let declaration outside extended mode或者SyntaxError: Unexpected identifier如果您没有处于严格模式,则可能是SyntaxError: Unexpected identifier ,或者可能是SyntaxError: Unexpected strict mode reserved word如果未启用Experimental JavaScript标记,则会出现SyntaxError: Unexpected strict mode reserved word

You can also compile your code with with Babel or with Traceur and the --block-binding flag enabled . 您还可以使用BabelTraceur编译代码,并启用--block-binding标志

See kangax's ES6 compatibility table for more. 有关更多信息,请参阅kangax的ES6兼容性表

Babel has a tool to test transpile and execute code. Babel有一个测试转换和执行代码的工具。 Just use it to test and in webapps it will very probable that you will use it to transpile to production code!! 只是用它来测试和在webapps中很可能你会用它来转换为生产代码!

http://babeljs.io/repl http://babeljs.io/repl

Babel tranpiler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM