Rewriting A WebApp With ECMAScript 6

Jan 29, 2015 · 11 min read


Today it’s possible for us to author in ES6 and transpile our sources down to ES5 at build-time, regardless of whether we’re using Grunt, Gulp or Broccoli. With projects like Ember and Angular looking to ES6 as part of their roadmaps too (Ember App Kit already supports ES6 modules!), now is a great time to learn how ES6 can be used in practice.

In this guide, we’ll re-write the well known TodoMVC application (implemented with Backbone.js) using ECMAScript 6 language semantics. The implementation is made possible using Google’s Traceur compiler and ES6-Module-Loader.

If you haven’t come across these tools before, Traceur is a compiler that allows you to use features from the future today and the ES6 Module Loader dynamically loads ES6 modules in Node.js and current browsers. If you’re wondering whether Traceur is ready for production-level apps, Erik Arvidsson recently answered this on their mailing list.

Features covered

Features we will be using in this walkthrough include but are not limited to:

Source & Demos

You can run the completed app, view a Docco version of the tutorial (app.html and todo-app.html) watch the project repository or look at the original ES5 implementation.

The application was rewritten in ES6 by Addy Osmani, Pascal Hartig, Sindre Sorhus, Stephen Sawchuk, Rick Waldron, Domenic Denicola and Guy Bedford.

Begin your ES6 adventure here

Our application is composed of primarily three files:

  • index.html — containing our templates and initial module loading.
  • app.js — our main entry point for the app
  • todo-app.js — our Todos module

It is also composed of stylesheets and of course our Backbone, Traceur and ES6 Module Loader dependencies, but we’ll focus on the ES6 portions of the application in this guide.

Index (index.html)

We first dynamically load our main application — a script located at js/app.js.

System.import is an asynchronous loader for loading such modules and doesn’t require us to include the extension to our script. This is automatically inferred:

The two script files we’ll be using for this application are js/app.js and js/todo-app.js. They are ES6 modules.

ES6 modules allow us to define isolated blocks of reusable code without having to wrap it into an object or closure. Only those functions and variables we explicitly export are available to other consumers and we can just as easily import functionality from other modules. It’s possible to rename exported values, define modules that are inline and even declare defaults for import/export.

Application entry-point (app.js)


We import the classes we defined in the TodoApp module using the import keyword.

Document ready

Arrow Functions (Statements)

We then load the application once the DOM is ready, using jQuery.ready () => { … } which you’ll see below is the statement form of the arrow function syntax. Practically speaking, it is lightweight sugar for function () { … }.bind(this).

Apart from containing statements instead of an automatically-returned expression, it has the same properties as the expression-form arrow functions we look at below:

TodoApp Module (todo-app.js)

Destructuring Assignments

Constant (const) declarations are block scoped, and cannot be re-declared or have their value reassigned once the binding is initialized.Backbone’s core component definitions don’t need to be modified, so we can combine constants and an ES6 pattern called destructuring assignment to create shorter aliases for Models, Views and other components.

This avoids the need to use the more verbose Backbone.* forms we’re accustomed to. Destructuring of array and object data uses a syntax that mirrors the construction of array and object literals.

const is currently disabled in Traceur due to but it would otherwise be written as:

We’ll use var here for now as we require a functional implementation:

Todo Model class


In JavaScript, we’ve relied on prototypal inheritance anytime we’ve needed a class-like system. This has led to overly verbose code using custom types. ES6 changes that by removing the ugly multi-step inheritance patterns we’re used to and introducing a minimal class syntax that makes defining classes a lot more terse.

ES6 classes desugar to prototypal inheritance behind the scenes and the only real change is that there’s less typing required for us. Classes are compact and we can use an ‘extends’ keyword to implement a new sub-class from a base-class. Below, we do this to define a Todo class which extends Backbone’s Model component.

TodoList Collection class

We define a new class TodoList extending Backbone’s `Collection. The collection of todos is backed by localStorage instead of a remote server.

Constructors and Super Constructors

Specifying a constructor lets us define the class constructor. Use of the super keyword in your constructor lets you call the constructor of a parent class so that it can inherit all of its properties.

Arrow Functions (Expressions)

The arrow (=>) is shorthand syntax for an anonymous function. It doesn’t require the function keyword and the parens are optional when there’s a single parameter being used. The value of this is bound to its containing scope, and when an expression follows the arrow — like in this case — the arrow function automatically returns that expression’s value, so you don’t need return.

Arrow functions are more lightweight than normal functions, reflecting how they’re expected to be used — they don’t have a prototype and can’t act as constructors. Because of how they inherit this from the containing scope, the meaning of this inside of them can’t be changed with call or apply.

To recap, when using =>:

  • The function keyword isn’t required.
  • Parentheses are optional with a single parameter.
  • return is unnecessary with a single expression.
  • Arrow Functions are lightweight — no prototypes or constructors

We use an arrow function below for our method that sets a Todo item as being completed:

Next, we look at filtering down the list to only todo items that are still not finished.

We keep the Todos in sequential order, despite being saved by unordered GUID in the database. This generates the next order number for new items.

Todos are sorted by their original insertion order using a simple comparator.

Create our collection of Todos.

Todo Item View class

We create a TodoView class by extending Backbone’s View as follows:

Next we setup a cached instance of our template and DOM listener events for individual items:

Re-render the contents of the todo item.

Accessor Properties

isHidden() is a get accessor property, but commonly referred to as a ‘getter’. Although technically part of ECMAScript 5.1, getters and setters allow us to write and read properties that lazily compute their values. Properties can process values assigned in a post-process step, validating and transforming during assignment.

In general, this means using set and get to bind a property of an object to a function which is invoked when the property is being set and looked up. Read more on accessor properties, or ‘getters and setters’.

Toggle the ‘completed’ state of the model.

Switch this view into ‘editing’ mode, displaying the input field.

Close the ‘editing’ mode, saving changes to the todo.

If you hit enter, we’re through editing the item.

Remove the item and destroy the model.

The Application class

Our overall AppView is the top-level piece of UI.

Instead of generating a new element, bind to the existing skeleton of the App already present in the HTML.

Delegate events for creating new items and clearing completed ones.

At initialization, we bind to the relevant events on the Todos collection, when items are added or changed. Kick things off by loading any preexisting todos that might be saved in localStorage.

Re-rendering the App just means refreshing the statistics — the rest of the app doesn’t change.

Add a single todo item to the list by creating a view for it, then appending its element to the <ul>.

Add all items in the Todos collection at once.

Generate the attributes for a new Todo item.

If you hit enter in the main input field, create a new Todo model, persisting it to localStorage.

Clear all completed todo items and destroy their models.

The Filters Router class

Default Parameters

param in the filter() function is using ES6’s support for default parameter values. Many languages support the notion of a default argument for functional parameters, but JavaScript hasn’t until now.

Default parameters avoid the need to specify your own defaults within the body of a function. We’ve worked around this by performing logical OR (||) checks against argument values to default if they’re empty/null/undefined or of the incorrect type. Native default parameter values provide a much cleaner solution to this problem. Notably they are only triggered by undefined, and not by any falsy value.

Compare the old way…

…to the new way, where we can also drop in Template String..

which we implement as follows:

And that’s it. Don’t forget you can check out the source for the application over on the project repository.

Web Components

Although we haven’t seen a huge amount of research into how ES6 modules will play with Web Components, Guy Bedford has been exploring this (via Polymer) and we’re looking forward to more examples of interop in the wild.

Tracking ECMAScript 6 Support

ECMAScript 6 is being progressively implemented by browser vendors over time and as such there is no ‘fixed’ date on it being available everywhere.

Whilst specs and implementations continue to finalize and ship, you may find the below resources helpful in keeping track of where we are with browser and environment support:

  • Feature comparison matrix of ECMAScript implementations (V8, JSC, JScript etc) by Thomas Lahn
  • Harmony features available in Node can be listed with node —v8-options | grep harmony
  • ECMAScript 6 compatibility table by Kangax
  • Google’s V8: Harmony features with corresponding open bugs in on the tracker (i.e what ES6 features Chrome and Opera will support, either by default or behind the about:flags > ‘Enable Experimental JavaScript’ flag).
  • ECMAScript 6 support in Firefox (MDN). There is also a meta-bug on Buzilla you can check-out that’s slightly more in the trenches (HT @domenic). In addition, Rick Waldron suggests subscribing for component notifications from bugzilla and then creating a filter, however this might be a little high-volume.
  • IE Dev Center on supported ES6 features (hunting down a better list for IE11+)
  • ECMAScript 6 official draft specification (and the unofficial HTML version of the draft)

We recommended that you follow @esdiscuss for a summary of what is happening on the es-discuss mailing list for links to the latest discussions.

Sindre Sorhus also maintains a list of real-world usage of ECMAScript 6 features in today’s libraries and frameworks that may be of interest.

ES6 Tools

If you’re interested in doing the deep-dive into ES6 today, the community has an evolving list of tools that can help with your workflow over at

We’ve also recently been playing with, which is awesome for experimenting with ES6 with zero-setup. Try it out!.

Wrapping up

In closing, we hope you found this brief walkthrough helpful. We’re pretty excited about seeing ES6 used to author more projects and invite you to share your own ES6 authoring experiences with us on Twitter.

With lots of ❤z,

Addy, Sindre, Pascal and Stephen.

With special thanks to Rick Waldron and Domenic Denicola for their reviews.

Originally published at on February 24, 2014.

TasteJS blog

A better JavaScript learning experience.


Written by


A better JavaScript learning experience. Keeper of TodoMVC & @PropertyCross. Eater of popsicles.

TasteJS blog

A better JavaScript learning experience. Keeper of TodoMVC & @PropertyCross. Eater of popsicles.


Written by


A better JavaScript learning experience. Keeper of TodoMVC & @PropertyCross. Eater of popsicles.

TasteJS blog

A better JavaScript learning experience. Keeper of TodoMVC & @PropertyCross. Eater of popsicles.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store