简体   繁体   中英

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.

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?

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:

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

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

And after testing the let block scope in a for loop

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

I got a syntax error:

SyntaxError: Unexpected identifier

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);
   }
})()

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.

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

See kangax's ES6 compatibility table for more.

Babel has a tool to test transpile and execute code. Just use it to test and in webapps it will very probable that you will use it to transpile to production code!!

http://babeljs.io/repl

Babel tranpiler

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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