| Start |
|---|
| Node ES6 Support and creating a project with Babel The whole ES6 spec is not yet implemented in its entirety so you will only be able to use some of the new features. You can see a list of the current supported ES6 features at http://node.green/ (opens new window) Since NodeJS v6 there has been pretty good support. So if you using NodeJS v6 or above you can enjoy using ES6. However, you may also want to use some of the unreleased features and some from beyond. For this you will need to use a transpiler It is possible to run a transpiler at run time and build, to use all of the ES6 features and more. The most popular transpiler for JavaScript is called Babel (opens new window) Babel allows you to use all of the features from the ES6 specification and some additional not-in-spec features with 'stage-0' such as If we wanted to create a project where we use 'stage-0' features such as import we would need to add Babel as a transpiler. You'll see projects using react and Vue and other commonJS based patterns implement stage-0 quite often. create a new node project Install babel the ES6 preset and stage-0
Create a new file called Note that we use an If you run Creating a .babelrc file in the root of your directory and add the following settings you can now run the server with Finishing off it is not a good idea to run a transpiler at runtime on a production app. We can however implement some scripts in our package.json to make it easier to work with. The above will on
Going one further you could then install nodemon This really speeds up working with babel and NodeJS. In you package.json just update the "dev" script to use nodemon
|
| Use JS es6 on your NodeJS app JS es6 (also known as es2015) is a set of new features to JS language aim to make it more intuitive when using OOP or while facing modern development tasks. Prerequisites:Here is a very short sample of a simple You can run this program and observe how it print the same message over and over again. Now.. let break it down line by line: This line is actually required if you intend to use js es6. Unbelievable - a When using OOP, a class is a very fundamental ability which assist the developer to represent a specific part of a system (breaking down code is crucial when the code is getting larger.. for instance: when writing server-side code) You got to admit - this is pretty intuitive! This is the c'tor of my class - this unique "function" will occur every time an object is created from this particular class (in our program - only once) Because print is defined in the class scope - it is actually a method - which can be invoked from either the object of the class or from within the class itself! So.. till now we defined our class.. time to use it: Which is truly equals to: In conclusion: JS es6 can simplify your code - make it more intuitive and easy to understand (comparing with the previous version of JS).. you may try to re-write an existing code of yours and see the difference for yourself ENJOY ???? |
Read Article
https://develop.horje.com/learn/1434/reference