ERRATUM: This an update of a previous post: Get ready for ECMAScript 6. During my investigation, I realized that there was an easier way to βmake ECMAScript 6. The update concerns only the use of bower and the Javascript references in html pages.
There is no impact about the next post: ECMAScript 6 in action with the inheritance and the models.
Get ready for ECMAScript 6
JavaScript evolves, and the next 6th version will be awesome, weβll have many great things like classes, maps, modules, β¦ (probably much more, but Iβm just starting to learn). There are several ways to begin writing ES6 source code (even now with shims, polyfills and transpilers), you can find a tools list here https://github.com/addyosmani/es6-tools.
I chose the simplest method to begin (to my mind) : no grunt task to transpile ES6 to ES5 or ES3, just some shims in the browser. But youβll still need a web server (http server). Personally I use a node script with Express, but other solution should be good like python command python -m SimpleHTTPServer
.
PS: you need bower for install shims. (and node and npm of course).
Caution: I try to improve my english, I hope this post will be understandable, but donβt hesitate to correct me (please).
Prepare the project
In a directory (ie: es6-project
) create 4 files:
.bowerrc
bower.json
package.json
(to declare Express dependencies)app.js
(our http server)
with these contents:
.bowerrc
bower.json
This is an update of the previous post: we only need of traceur.js
Remark: uikit isnβt mandatory, it is only for pretty pages.
package.json
app.js
And now install all of this!
- to download es6 shims: type
bower install
- to install Express: type
npm install
Thatβs all. Now, you should have something like this:
es6-project/
βββ node_modules/
βββ public/
| ββββ bower_components/
βββ .bowerrc
βββ bower.json
βββ package.json
βββ app.js
Create a js
directory, with an app
sub-directory in public
, and a main.js
file in app
directory and an index.html
file in public
:
es6-project/
βββ node_modules/
βββ public/
| βββ bower_components/
| βββ js/
| | βββ app/
| | βββ main.js
| βββ index.html
βββ .bowerrc
βββ bower.json
βββ package.json
βββ app.js
Some code
# Prepare html
Modifiy index.html
like that:
This is an update of the previous post: we only need of traceur.js
# Application class
We are going to write our first ES6 class. So in the main.js
file, type this content:
This is our main class, the first βthingβ that will be run in our webapp.
# βIgnitionβ
We need to declare our main class in index.html
, just add this after scripts declarations:
So, your index.html
should look like to this:
This is an update of the previous post: we only need of traceur.js
You can now check that everything works:
- run Express application:
node app.js
- open http://localhost:3000 with your brower.
Hopefully, you shoul obtain a βvery niceβ message E6 rocks!
.
Now you have a βrunningβ project and you can go deeper.
Go deeper: more classes
Create a models
directory in publix/js/app
with two new JavaScript files inside human.js
and humans.js
:
es6-project/
βββ node_modules/
βββ public/
| βββ bower_components/
| βββ js/
| | βββ app/
| | βββ models/
| | | βββ human.js
| | | βββ humans.js
| | βββ main.js
| βββ index.html
βββ .bowerrc
βββ bower.json
βββ package.json
βββ app.js
Human class
Imagine Human class as a kind of model:
export
keyword is very important, we βexposeβ Human class, so we can declare/load it from other JavaScript files.
Humans class
We could say that Humans class is a collection of humanβs models:
Using Human and Humans in Application class
We need to declare our 2 classes to use them. You have to use import
keyword:
And now, you can instantiate models and collections:
And if you want to display the collection content, before add <ul></ul>
in the body of index.html
and write this JavaScript code to populate the list (<ul></ul>
):
Remark:String interpolation (as with Coffeescript) is a very handy feature of ES6, note the use of the symbol ` (back ticks) instead of double or single quotes to declare a βtemplate stringβ.
So at the end, you Application should look like this:
You have now a functional ECMAScript 6 project, you can play!
Tweet