How to start using ES6 now?
it's possible to use ES6 today while still targeting older browsers. This is possible thanks to "transpilers"
that can convert ES6 code to ES5, we can implement a transpiler into our development workflow and use ES6 that's goign to be automaticly transform into ES5 using Gulp or Grunt .
npm install --save-dev -g babel
babel-node
[1,2,3].map(x => x * x); // [ 1, 4, 9]
"use strict";
[1, 2, 3].map(function (x) {
return x * x;
});
const sum = arr => arr.reduce((a, b) => a + b);
console.log( 'sum: ', sum([1,2,3]) )
'use strict';
var _temporalUndefined = {};
var sum = _temporalUndefined;
function _temporalAssertDefined(val, name, undef) { if (val === undef) { throw new ReferenceError(name + ' is not defined - temporal dead zone'); } return true; }
sum = function sum(arr) {
return arr.reduce(function (a, b) {
return a + b;
});
};
console.log('sum: ', (_temporalAssertDefined(sum, 'sum', _temporalUndefined) && sum)([1, 2, 3]));
git clone https://github.com/google/traceur-compiler.git
cd traceur-compiler
npm install
make
// go to:
cd example
open hello.html
// Now let edit the ES6 JS:
example/hello.html
/traceur-compiler/
./traceur --version
nano test.js
class Greeter {
sayHi(name = 'Anonymous') {
console.log(`Hi ${name}!`);
}
}
var greeter = new Greeter();
greeter.sayHi();
sudo ./traceur --out out/test.js --script test.js
/out/test.js
// we are going to use the: 'out/test.js'
<html>
<head>
<script src="bin/traceur.js"></script>
<script src="out/test.js"></script>
</head>
<body>
Check your console…
</body>
</html>
npm install
babel-node point.js // My point: {"x":7,"y":4}