2015
Source: http://kangax.github.io/compat-table/es6/
What's new in ES6 ?
Classes
Modules
Arrow
functions
Template Strings
Constants
Symbols
Shorthand
Object literals
Collections
Promises
Destructuring
Default/Rest/
Spread parameters
Iterators
Generators
Proxies
Reflect API
Why not?
// Expression bodies
var array = [1, 2, 3, 4];
var odds = array.filter(v => v % 2 == 1);
var squares = evens.map(v => v * v);
// Statement bodies
$.put(url, (response) => {
$('#div').text(response.data);
console.log(response);
});
$('#button').on('click', (e) => {
e.preventDefault();
console.log('clicked...');
})
// Lexical scope
MyClass.prototype.save = function () {
setTimeout( () => {
this.data = 'done'; // this is bound to MyClass, not to window
}, 1000)
}
// 1. Event callbacks
$scope.$on('selection:deselect:all', function (event, args) {
changeAllRowsSelectState(false);
});
// 2. Set Timeout callbacks
setTimeout(function() {
window.onbeforeunload = onBeforeUnloadHandler;
}, 0);
// 3. Lexical scope using self
var self = this;
$http.post('/widgets').success(function (resp) {
self.archive(row.id);
// ...
})
// 1. Event callbacks
$scope.$on('selection:deselect:all', () => changeAllRowsSelectState(false));
// 2. Set Timeout Callbacks
setTimeout(() => window.onbeforeunload = onBeforeUnloadHandler);
// 3. Lexical scope
$http.post('/widgets').success( (resp) => this.archive(row.id) ); // no need for selfies
function Foo() {
this.foo = 'foo';
}
var bar = 'foobar';
var myObject = {
__proto__: Foo.prototype, // Set the object's prototype to be Foo's
bar, // Shorthand for bar: bar
toString () { return super.foo + this.bar }, // Shorthand methods
['prop_' + bar]: bar // Create a dynamic property of name "prop_foobar"
}
angular.module('dyApp').factory('EvaluatorsService', ['$http', '$q', function ($http, $q) {
return {
get: function (type) {
// ...
},
archive: function (id) {
// ...
}
}
}])
// ==>
angular.module('dyApp').factory('EvaluatorsService', ['$http', '$q', ($http, $q) => {
return {
get (type) { // ... },
archive (id) { // ... }
}
}])
function Foo() {
this.foo = 'foo';
}
var bar = 'foobar';
var myObject = {
__proto__: Foo.prototype, // Set the object's prototype to be Foo's
bar, // Shorthand for bar: bar
toString () { return super.foo + this.bar }, // Shorthand methods
['prop_' + bar]: bar // Create a dynamic property of name "prop_foobar"
}