ECMAScript is the standardized specification of the scripting language that is the basis for JavaScript implementations in web browsers.
ECMAScript 5, which was released in 2009, is in widespread use today. Six years later in 2015 ECMAScript 6 was released, which introduced a lot of new features, especially additions to the syntax.
However, browser support for ES 6 needed some time to catch up.
If you look at the compatibility table for ES 6 support in modern browsers you can see that the current versions of the main browsers now support ES 6. This means that soon you will be able to write ES 6 code without the necessity of a ES6-to-ES5 cross-compiler like Babel.
The two features of ES 6 that will change the way JavaScript code will be written are in my opinion: the new class syntax and the new lambda syntax with lexical scope for ‘this’.
Class syntax
Up to now, if you wanted to define a new type with a constructor and methods on it, you would write something like this:
var MyClass = function(a, b) { this.a = a; this.b = b; }; MyClass.prototype.methodA = function() { // ... }; MyClass.prototype.methodB = function() { // ... }; MyClass.prototype.methodC = function() { // ... };
ES 6 introduces syntax support for this pattern with the class keyword, which makes class definitions shorter and more similar to class definitions in other object-oriented programming languages:
class MyClass { constructor(a, b) { this.a = a; this.b = b; } methodA() { // ... } methodB() { // ... } methodC() { // ... } }
Arrow functions and lexical this
The biggest annoyance in JavaScript were the scoping rules of this. You always had to remember to rebind this if you wanted to use it inside an anonymous function:
var self = this; this.numbers.forEach(function(n) { self.doSomething(n); });
An alternative was to use the bind method on an anonymous function:
this.numbers.forEach(function(n) { this.doSomething(n); }.bind(this));
ES 6 introduces a new syntax for lambdas via the “fat arrow” syntax:
this.numbers.forEach((n) => this.doSomething(n));
These arrow functions do not only preserve the binding of this but are also shorter. Especially if the function body consists of only a single expression. In this case the curly braces and and the return keyword can be omitted:
// ES 5 numbers.filter(function(n) { return isPrime(n); }); // ES 6 numbers.filter((n) => isPrime(n));
Wide-spread support for ECMAScript 6 is just around the corner, it’s time to familiarize yourself with it if you haven’t done so yet. A nice overview of the new features can be found at es6-features.org.