现在我如何编写和测试ECMAScript 6代码? [英] How can I write and test ECMAScript 6 code now?

查看:235
本文介绍了现在我如何编写和测试ECMAScript 6代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开始使用即将到来的ECMAScript 6(ES6)编写代码,以便开始掌握新的语法。

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.

是否有一些我可以使用一些网络资源或浏览器插件来播放(编写和测试代码)关于我们目前对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?

我率先相信使用Google Chrome Canary可能有帮助。所以我下载了Canary,我启用了金丝雀的几个功能:

I was lead to believe that using Google Chrome Canary might be able to help. So I downloaded Canary, I enabled a couple of features in Canary:


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

Enable Experimental JavaScript (Mac, Windows, Linux, Chrome OS, Android)

启用网页以使用实验性JavaScript功能。

#enable-javascript -harmony 启用

Enable web pages to use experimental JavaScript features.
#enable-javascript-harmony Enable

测试在for循环中阻止范围

And after testing the let block scope in a for loop

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

我有一个语法错误:


SyntaxError:意外的标识符

SyntaxError: Unexpected identifier


推荐答案

以下内容在Chrome 37(当前Chrome)中启用了实验JavaScript标志:

The following works in Chrome 37 (current Chrome) with the Experimental JavaScript flag enabled:

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

在严格模式之外,您应该看到 SyntaxError:非法的扩展模式下的声明 SyntaxError:意外的标识符如果您不在严格模式,或可能 SyntaxError:意外严格模式保留字如果实验JavaScript标志未启用。

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.

您还可以使用 Babel 或使用 Traceur和 - 阻止绑定标志启用

You can also compile your code with with Babel or with Traceur and the --block-binding flag enabled.

请参阅 kangax的ES6兼容表了解更多。

这篇关于现在我如何编写和测试ECMAScript 6代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆