In part one of this miniseries, we talked about the timeline for ES6 rollout, feature compatibility in existing environments and transpilers, and how to get ES6 set up in your build process.
Today, we’ll continue the conversation, looking at some of the easiest places to start using ES6 in a typical front-end Backbone + React project. Even if that's not your stack, read on! There's something for everyone here.
If you want to try out the examples, you can use a sandboxed ES6 environment at ES6 Fiddle.
Classes, Shorthand Methods, and Shorthand Properties
A lot of client-side JS code is object-oriented. If you're using Backbone, just about every Model, Collection, View, or Router you ever write will be a subclass of a core library Class. With ES6, extending these objects is a breeze. We can just call
class MySubclass extends MyClass and we get object inheritance. We get access to a
constructor method, and we can call
super from within any method to apply the parent class's method of the same name. This prevents us from having to write things like: