Posts

Showing posts from March, 2015

ES6 generators in depth

Image
This blog post is outdated. Please read chapter “ Generators ” in “Exploring ES6”. This blog post is part of a series on iteration in ES6: Iterables and iterators in ECMAScript 6 ES6 generators in depth Generators, a new feature of ECMAScript 6 [4] , are functions that can be paused and resumed. This helps with many applications: iterators, asynchronous programming, etc. This blog post explains how generators work and gives an overview of their applications. The following GitHub repository contains the example code: generator-examples Overview Two important applications of generators are: Implementing iterables Blocking on asynchronous function calls The following subsections give brief overviews of these applications, more thorough explanations are provided later (plus discussions of other topics). Implementing iterables via generators The following function returns an iterable over the properties of an object, one [key,value] pair per property: // The asterisk after `function` ...

Using the ES6 transpiler Babel on Node.js

This blog post is outdated (it covers Babel 5). Please read Sect. “ Node.js setup: Dynamically transpiled ES6 via Babel ” in “Setting up ES6”. This blog post explains how to use the ES6 transpiler Babel with Node.js. You can download the code shown in this post on GitHub. For further information on ECMAScript 6, consult the ebook “ Exploring ES6 ”. Warning: The approach explained in this post is convenient for experiments and development. But it uses on-the-fly transpilation, which may be too slow for your production code. Then you can transpile as a build step ( as explained in the Babel documentation ). Running normal Node.js code via Babel The npm package babel brings Babel support to Node.js: $ npm install --global babel This package contains the shell script babel-node , which is a Babel-ified version of node . It compiles everything from ES6 to ES5 that is run or required. For example, you can start a REPL via the following shell command: $ babel-node In the REPL, you...

The destructuring algorithm in ECMAScript 6

This blog post is outdated. Please read Sect. “ The destructuring algorithm ” in “Exploring ES6”. This blog post looks at destructuring from a different angle: as a recursive matching algorithm. At the end, I’ll use this new knowledge to explain one especially tricky case of destructuring. Destructuring This section gives a brief overview of destructuring. For further details, consult the blog post “ Destructuring and parameter handling in ECMAScript 6 ”. The following code is an example of destructuring: let obj = { first: 'Jane', last: 'Doe' }; let { first: f, last: l } = obj; // (A) // f = 'Jane'; l = 'Doe' In line (A) we destructure obj : we extract data from it via a pattern on the left-hand side of the assignment operator ( = ) and assign that data to the variables f and l . These variables are automatically declared beforehand, because the line starts with a let . You can destructure arrays, too: let [x, y] = ['a', ...