IE Technical Preview | 69% |
Firefox 37 | 68% |
Chrome 42 | 48% |
io.js | 47% |
NodeJS | 29% |
6to5 | 78% |
Traceur | 61% |
ES6 Transpiler | 26% |
function increment(x, step) {
if(typeof step === "undefined") {
step = 1;
}
return x + step;
}
function increment(x, step = 1) {
return x + step;
}
increment(3) == 4
increment(3, 10) == 13
function insertLog(title, time = Date.now()) {
...
}
var firstname = "John", lastname = "Connor";
var obj = {
firstname : firstname,
lastname: lastname,
toString: function() {
return this.firstname + ' ' + this.lastname;
}
};
obj["id_" + getId()] = 42;
var firstname = "John", lastname = "Connor";
var obj = {
firstname, // => firstname: firstname
lastname,
toString() {
return this.firstname + ' ' + this.lastname;
},
[ "id_" + getId() ]: 42
};
var piecesPerPie = 6;
var numberOfPies = 3;
console.log('There are ' + numberOfPies + ' pies, so we have ' +
(piecesPerPie * numberOfPies) + ' pieces.');
var piecesPerPie = 6;
var numberOfPies = 3;
console.log(`There are ${numberOfPies} pies, so we have \
${piecesPerPie * numberOfPies} pieces.`);
function renderPerson(person) { return `
<dl>
<dt>First</dt> <dd>${person.first}</dd>
<dt>Last</dt> <dd>${person.last}</dd>
</dl>
`;
}
renderPerson({first: 'Brian', last: 'Genisio'});
var odds = evens.map(function(v) { return v + 1 });
nums.forEach(function(v) {
if (v % 5 === 0)
fives.push(v);
});
var shop = {
_items: ['iPhone 6', 'iPad Air', 'Nexus 9', 'Kindle Fire'],
bindEvents: function() {
var that = this;
$('#display').click(function(){ // this == button element
that._items.forEach(function(i){
$('#products').append('<div>' + i + '</div>');
});
});
}
}
var odds = evens.map(v => v + 1);
nums.forEach(v => {
if (v % 5 === 0)
fives.push(v);
});
var shop = {
_items: ['iPhone 6', 'iPad Air', 'Nexus 9', 'Kindle Fire'],
bindEvents: function() {
$('#display').click((ev) => { // this == shop object
this._items.forEach(i => $('#products').append('<div>' + i + '</div>');
});
}
}
var func = () => "";
var instance = new func(); // TypeError: func is not a constructor
func.prototype; // undefined
function sum(...numbers) {
var sum = 0;
numbers.forEach(number => {
sum += number;
});
return sum;
}
sum(1, 2, 3); // == 6
sum(); // == 0
function sum(head, ...tail) {
if(!tail.length) return head;
return head + sum(...tail);
}
sum(1, 2, 3); // == 6
sum(); // == 0
var [a, b] = [1,2];
// a == 1, b == 2
var [a, b] = [b, a];
// a == 2, b == 1
function getArray(){
return [1,2,3,4];
}
var [a,,c,d] = getArray();
// a == 1, c == 3, d == 4
var {firstname: fn, lastname: ln} =
{firstname: 'Bruce', lastname: 'Wayne'};
// fn == 'Bruce', ln == 'Wayne'
function getResult() {
return {
result: 1234,
error: null
};
}
var {result, error} = getResult();
// result == 1234, error == null
var options = {
elementId: 'content',
className: 'blue-content',
position: [100,200],
styles: {
color: 'blue'
}
};
var {
elementId: el,
position: [x,y],
styles : { color: color }
} = options;
// el == 'content', x == 100, y == 200, color == 'blue'
function fnScoped() {
if(true) {
var zed = 'alive'; // zed is "hoisted" to the function block
}
if(true) {
console.log('zed is ' + zed); // 'zed is alive'
}
}
if(true) {
let zed = 'alive';
}
console.log('zed is ' + zed); // ReferenceError: zed is not defined
for(var i = 0; i < 10; i++) {
var button = $('<button>Button #' + i + '</button>');
button.click(function() { alert(i); });
$('body').append(button);
}
var i;
for(i = 0; i < 10; i++) {
var button = $('<button>Button #' + i + '</button>');
button.click(function() { alert(i); });
$('body').append(button);
}
// i == 10
// button.click => alert(i);
for(let i = 0; i < 10; i++) {
let button = $('<button>Button #' + i + '</button>');
button.click(function() { alert(i); });
$('body').append(button);
}
const MAX_ITEMS = 30;
// Syntax error: missing initialization
const NAME;
if (condition) {
const BASE_URL = 'http://www.yahoo.com';
...
}
// BASE_URL isn't accessible here
const foo = {};
foo.prop = 123; // OK, the object is mutable
foo = {}; // TypeError
const bar = [];
bar.push('abc'); // OK, the array is mutable
var arr = [1,2,3,4];
for(var i=0; i<arr.length; i++) {
console.log(arr[i]);
}
arr.forEach(function(){ console.log(i); });
let arr = [1, 2, 3, 4];
for(let i of arr){
console.log(i);
}
// 1, 2, 3, 4
function Person(name) { this.name = name; }
Person.prototype.hi = function () {
return "Hello " + this.name;
};
function Employee(name, title) {
Person.call(this, name);
this.title = title;
}
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
Employee.prototype.hi = function () {
return Person.prototype.hi.call(this)
+ ", the " + this.title;
};
YUI | has Y.extend() to perform inheritance. |
---|---|
Prototype | has Class.create() and Object.extend() for working with objects and “classes”. |
Dojo | has dojo.declare() and dojo.extend(). |
MooTools | has a custom type called Class for defining and extending classes. |
... |
class Person {
constructor(name) {
this.name = name;
}
hi() {
return `Hello ${this.name}`;
}
}
let p = new Person('Walter');
p.hi(); // Hello Walter
class Employee extends Person {
constructor(name, title) {
super.constructor(name);
this.title = title;
}
hi() {
return `${super.hi()}, the ${this.title}`;
}
}
let p = new Employee('W. White', 'Teacher');
p.hi(); // Hello W. White, the Teacher
{
let transformObject = x => x;
let inst = new MyClass(); // ReferenceError
// Note the expression in the `extends` clause
class MyClass extends transformObject(Object) {
}
}
<script src="lib/math.js"></script>
<script src="lib/jquery.js"></script>
<script>
$('#pi').text(math.sum(math.pi, math.pi));
</script>
require(["lib/math", "lib/jquery"],
function(math, $) {
$('#pi').text(math.sum(math.pi, math.pi));
}
);
var math = require('lib/math'),
$ = require('lib/jquery');
$('#pi').text(math.sum(math.pi, math.pi));
> browserify main.js -o bundle.js
<script src="bundle.js"></script>
export function sum(x, y) {
return x + y;
}
export const PI = 3.141593;
import * as math from "lib/math";
import {jQuery as $} from "lib/jquery";
$('#pi').text(math.sum(math.PI, math.PI));
System.import('lib/math').then(math => {
$('#pi').text(math.sum(math.PI, math.PI));
});
function* generatorFunction() {
yield 1;
yield 2;
}
let genObj = generatorFunction();
genObj.next(); // { done: false, value: 1 }
genObj.next(); // { done: false, value: 2 }
genObj.next(); // { done: true }
function* generatorFunction() { yield 1; yield 2; } let genObj = generatorFunction(); genObj.next(); // { done: false, value: 1 } genObj.next(); // { done: false, value: 2 } genObj.next(); // { done: true }
function* gen() {
yield 1;
yield 2;
}
for(let i of gen()){
console.log(i);
}
// 1, 2
function* traverse(node) {
if (Array.isArray(node)) { // node
for(let i=0; i < node.length; i++) {
yield* traverse(node[i]);
}
} else {
yield node; // leaf
}
}
const tree = [ 'a', ['b', 'c'], ['d', 'e'] ];
for(let x of traverse(tree)) {
console.log(x);
}
function* longRunningTask() {
// do something
yield; // pause
// do something
yield; // pause
}
let task = longRunningTask();
while(!task.next().done) {
console.log('Next Step...');
}
console.log('Done.');
$.ajax(url, {
success: function(data) {
$('#result').html(data);
var status = $('#status').html('Download complete.');
status.fadeIn({
complete: function(){
setTimeout(function(){
status.fadeOut();
}, 2000);
}
});
}
});
import spawn from "http://taskjs.org/es6-modules/task.js";
spawn(function*() {
var data = yield $.ajax(url);
$('#result').html(data);
var status = $('#status').html('Download complete.');
yield status.fadeIn().promise();
yield sleep(2000);
status.fadeOut();
});
> npm install --global 6to5
> 6to5 script.js > build.js
<script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script>
<script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script>
<script type="module">
class Greeter {
constructor(message) {
this.message = message;
}
greet() {
console.log(this.message);
}
};
var greeter = new Greeter('Hello, world!');
greeter.greet();
</script>
> 6to5 script.js --out-file build.js --source-maps
Farshad Sepehrarafarshads@systemgroup.net |
Yousef Salimpouryousefs@systemgroup.net |