There were plenty of other parts explained that I’d never seen in the wild before. For me these are the highlights of the book.
In es6 instantiated objects can be inspected with a built-in object called
Reflect. With Reflect you can amongst others get, set, check attributes and call functions of/on an object. At first glance this doesn’t look very exciting, but you can get creative with this. Also you can get or set the prototype of any object which has large implications which will be explained later in this article.
Classes are still prototypes
Symbols, a new primitive type
Using objects as iterators
Besides arrays you can define an instance to be an iterable object. This object implements the Iterator Protocol meaning it’s required to have a
next() method that returns an object with a
done and a
value property. When the
done property is
true the loop stops iterating. The
value property is the value in the current iteration.
For generating values. Requires at least a yield otherwise the *function will raise an error. The responsibility of looping through values is put on the caller instead of the generator function. This means a generator function can hold an infinite loop while the caller determines how many values of the infinite loop it needs.
Tags are functions that receive the contents of the provided template literal in 2 parts: the static string parts and the variable values both presented in an array. After disassembled the template literal can be reassembled in another way of your choosing.
It was always possible to have variable function names for object members. However with object literals readability has improved and variable functions don’t have to be called like
obj[‘variableFunctionName’]() , instead they are treated as computed properties inside the object resulting in a natural way of calling
Meta programming with Proxy
A proxy is another built-in object that changes the behaviour of get, set and function call operations of an object dynamically based on the current state at runtime. With a proxy it’s possible to hijack a function and perform some extra code before, after or around the original function. This may be desirable when original legacy code needs to be left intact while requiring some modifications in its behaviour.
reflect-metadata library is used to define meta data on the base Component. The data can be set when decorating another component.
Even though it’s not always obvious what kind of problems these features can solve I’m sure I will run into situations sooner or later where these will come in handy. Besides that, knowing these help me understand what’s actually going on under the hood when reading code and syntax used by some frameworks won’t look like black magic to me any more. (D)