Last active
August 29, 2015 14:26
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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