A glimpse at some ES6 features used quite frequently.
Spread Syntax
Spread syntax, as the name suggests, is used to spread an array, a string, or an object wherever zero or more arguments(function calls), elements(array literals), or key-value pairs(object literals) are needed.
Note that object spread syntax is not ES6, but will be standardized in ECMAScript 2018.
1 | function add(x, y) { |
Replace apply()
In ES5, apply() is used convert array into arguments. With spread syntax, there is no need for that.
1 | // ES5 |
Copy arrays or objects
- Array
1 | var arr = [1, 2, 3]; |
Note that spread syntax effectively goes one level deep while copying an array, so it may not be suitable to copy multidimensional arrays.
1 | var a = [[1], [2], [3]]; |
- Object(ES7)
Spread syntax copies own enumerable properties from a provided object onto a new object. Note that it is shallow copy(excluding prototype).
Shallow-cloning or merging of objects is now possible using a shorter syntax than Object.assign()
.
1 | var obj1 = { foo: 'bar', x: 42 }; |
Integration with destructing assignment
1 | // ES5 |
String Literals
1 | [...'hello'] |
Other iterables(any object implementing Iterator Interface)
Here querySelectorAll
is a nodeList
object, which implements the Iterator
Interface.
1 | let nodeList = document.querySelectorAll('div'); |
Rest Parameter
Rest parameter is used when we have indefinite number of arguments passed to an array. It is generally considered a replacement for arguments object in previous version.
1 | function f(a, b, ...theArgs) { |
A few points to note:
- Unlike arguments, rest parameters are stored in a real array.
- There should be no more arguments after rest parameters.
1 | // Error! |
- The array length does not take rest into account.
1 | (function(a) {}).length // 1 |
- Rest can be used together with destructing assignment
1 | function f(...[a, b, c]) { |
Destructing Assignment
The destructuring assignment syntax is used to unpack values from arrays, or properties from objects, into distinct variables.
Note: It is very common to use destructing assignment with rest parameter together.
Array Destructing
1 | let a, b, rest; |
Object Destructing
In array destructing, we use indexing to do match left-hand and right hand. In object destructing, however, there is such ordered indexing. As a result, we need to provide key in left hand side to match the right hand side.
1 | ({ a, b } = { a: 10, b: 20 }); |
What if we want assign the value to a variable with new name?
1 | let o = {p: 42, q: true}; |
Default values
A variable can be assigned a default, in the case that the value unpacked from the array/object is undefined.
1 | // array |
Module
Review of node.js module.exports
and require
As a comparision, let us see how we manage modules in node.js:
1 | // Example 1 |
Note the above syntax is still valuable in certain situtations when we could only use ES5(like in webpack config files).
Module in ES6 with import and export
Great news: In ES6, JS now natively supports modules!
The same code here, except module.exports
is replaced with export
, and require
is replaced with import
1 | // Example 1 |
The reason behind ES6 modules
Previously in CommonJS module(module.exports
and require
), modules are loaded at running time. Because of that, a lot of code optimizations are impossible.
What people have in mind when designing ES6 that work should be moved to compilation time as much as possible. So here comes ES6 modules, which provide much more opportunities for optimizations(for example, macro and type system).
export default
This command sets the default exported interface. With this, users do not need to the name of interface they want to load before hand.
1 | // export-default.js |
In essence, export default
is a convenient way to create a interface named dafault and export it. And when import, the JS engine will look for the interface named default and loads it.
Note that export default
differs a little in syntan with export
:
1 | // example1 |
Template Literals
Template literals are string literals allowing embedded expressions.
1 | `string text` |
Computed property names
In ES6, the object initializer syntax supports computed property names. This is like we used to with property accessor syntax.
1 | // property accessor for comparision |
Default parameters
Another syntax sugar in ES6.
1 | // how we do it in pre ES6 |