Skip to content

Instantly share code, notes, and snippets.

@ingro
Last active August 29, 2015 14:26
Show Gist options
  • Save ingro/3f8c0dec3e0a30b8b476 to your computer and use it in GitHub Desktop.
Save ingro/3f8c0dec3e0a30b8b476 to your computer and use it in GitHub Desktop.
ES5 vs ES6 - For a complete list see here: https://github.com/lukehoban/es6features
// Short syntax
// ES6
var game = {
start() {
console.log('START!');
}
}
// ------------------------------
// Fat arrow
// ES5
var self = this;
_.filter(items, function(item) {
self.add(item.total);
});
// ES6
_.filter(items, (item) => {
this.add(item.total);
});
// Template strings
// ES5
console.log('Hello ' + name);
// ES6
console.log(`Hello ${name}`);
// ----------------------------------------
// Dynamic object properties
var name = 'foo';
var game = {
[`log_${name}`](): {
console.log(name)
}
}
game.log_foo();
// game.js
var game = {
start: function() {
console.log('STARTING');
},
end: function() {
console.warn('END');
}
}
// ---------------------------------------
// Requirejs
require('game', function(game) {
game.start();
});
// ES6
import game from 'game';
game.start();
// But also
import { start } from 'game';
start();
// Default parameters
// ES5
function start(seconds) {
seconds = seconds || 5;
console.log(seconds);
}
// ES6
function start(seconds = 5) {
console.log(seconds);
}
// -----------------------------------
// Destructing
// ES5
var port = 3000;
var host = 'localhost';
var options = {
host: host,
port: port
};
function connect(options) {
var port = options.port;
var host = options.host;
}
// ES6
var port = 3000;
var host = 'localhost';
var options = {
host,
port
};
function connect(options) {
var { port, host } = options;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment