ES6: ECMAscript Strikes Back

For the second edition of my ES6 blog trilogy, we are going to take a quick peek at some really cool features such as, symbols, maps, collections, default parameters, and classes! Again, if you haven’t seen it yet, check out Aaron Frost’s ES6 talk on Pluralsight.

Classes

Classes provide a new syntactical way to create classes and subclasses that is both intuitive and easy to follow. It is important to note that the new class feature offers no new functionality or performance gains (as of yet). Everything you can do with classes you can also do with the current pseudoclassical instantiation pattern and prototype chaining. Classes, however, will make classing and subclassing much easier.

So let’s dive in!

Class Constructor and prototype methods

As you can see, the syntax is a lot more explicit. The class handles its methods within its initial instantiation rather than adding individual methods to a prototype after instantiating the constructor. To me, this is a lot more familiar to other languages I’ve worked with before javascript.

Getters and Setters

The new class syntax will also utilize the keyword get and set. A function using the get keyword is written like a function, but treated like a property.

As you can see, even though the getter and setter functions were written like functions, they were executed like properties. Having a getter function allows an outsider to access properties, including those that may have been hidden or private, and a setter allows outsiders to set or influence those properties in some way. It is an important thing to understand as it allows the developer to explicitly decide what a user can actually touch or change.

This is where the new class system really shines. Subclassing was never difficult in javascript, but the way one went about doing it was a bit obscure. With the new class syntax, creating a subclass is as simple as it gets. Let’s take a look

Symbols and Private Variables

Symbols offer an exciting way to handle data that you don’t necessarily want to share. It creates a seemingly random set of letters and numbers that represent a key. A symbol is naturally innumerable, meaning it won’t show up when you iterate through keys or check for length, and a symbol cannot be changed in memory.

As you can see, there is a heavy dose of obscurity when viewing properties from the outside using symbols. From the perspective of the developer, we will only be dealing with named keys, but if someone looks from the outside, all they see is a strange series of numbers that change every time a new instance is built.

This is really where the getters and setters become important, as you can explicitly provide access to some of these private variables

Of course, to really make this class constructor secure, we need to wrap the entire thing into a closure so no one can peek at where we are instantiating our symbols and figure out what they mean. To do this, we will throw the entire constructor into an IIFFE — immediately invoked function expression.

Default Parameters

How many times have you had to write a function like this?

It is a common short circuit evaluation that allows us to provide a default parameter. Now, with ES6, there is an explicit technique for setting default parameters.

As you can see, the default parameter only activates when the parameter is undefined. It won’t activate on a falsey statement.

Default parameters can handle function calls as well, which will only be executed if the variable passed in is undefined

Default parameters cannot be used on splat arguments. Such a declaration would return an error

Collections

ES6 is going to offer three new collections that are going to offer significant improvements to the way we do things.

SET

A set is very similar to an Array.
SET has various new methods such as has, add, clear, delete

All items in a SET are unique

However, there is no typecasting on uniqueness, meaning elements won’t automatically switch to an integer or string

Most importantly, Sets have iterators in them which allow us to iterate over the values in a new, very cool way.

The new of keyword automatically iterates over every single element in a SET.

Map

A Map is very similar to a javascript object. To add values, we use the .set method and check to see if a key/value exists with the .has method. Like SETS, there is no typecasting in Maps, so the integer 1 is an entirely different key than the string ‘1’ which is functionally opposite to the way normal objects work.

Using Objects as Keys
This is where maps really shine. Maps allow us to use javascript objects as keys which will have huge advantages when dealing with serverside data. Let’s dive into an example:

So instead of having to parse through a JSON object to find whatever value we chose to use as a key, we can just pass in their entire user object as the key, and the value it is linked to will be returned.

It is important to note, however, that to access a Map, we must use the same exact key. Having two variables with the exact same data will not offer access to the map, as the key is actually the pointer, not the data itself
for example:

There is one major problem with the Map. If we store something into the Map, say a user, and then delete the user. That user will never be garbage collected because it still exists in the Map. This can lead to major memory problems down the road. Which is why they built our last collection –>

WeakMaps

Weakmaps are almost exactly like Maps. In fact they pretty much work in the same way with one major difference. If a variable is used as a key in Weakmaps, and it is the only place where that variable exists (like using a user as a key and then deleting the user), the weakmap will automatically let it go and let it be trash collected. As a result, a weakmap has

  1. No primitive Keys
  2. No .Size method

And of course, they are created like this:

That’s it for this episode. Tune in next week for the trilogy finale when we dive into Promises and other exciting topics of ES6!

Originally published at questhenkart.com on May 25, 2015.

Software Architect