Support many ES6 features with no runtime requirements. Sometimes this results in more code, but it provides a much cleaner build system. Things like Map
, Set
, Symbol
etc. must be polyfilled separately if you wish to use them. This module only aims to provide syntax extensions.
npm install acorn-es6
var compile = require('acorn-es6');
var compiled = compile('var log = msg => console.log(msg);');
// => "var log = function (msg) { console.log(msg); };"
You can also use it as a browserify transform by specifying the transform as acorn-es6/browserify
.
var log = msg => console.log(msg);
function logDeveloper(name, codes = 'JavaScript', livesIn = 'USA') {
console.log('name: %s, codes: %s, lives in: %s', name, codes, livesIn);
}
for (var element of [1, 2, 3]) {
console.log('element:', element);
}
var object = {
prop: 42,
// No need for function
method() {
return this.prop;
}
};
var x = 5, y = 10;
console.log(`${x} + ${y} = ${ x + y}`);
// 5 + 10 = 15
function printList(listname, ...items) {
console.log('list %s has the following items', listname);
items.forEach(function (item) { console.log(item); });
}
function add(x, y) {
console.log('%d + %d = %d', x, y, x + y);
}
var numbers = [5, 10]
add(...numbers);
// 5 + 10 = 15
MIT