A while ago, I was prototyping around the idea of a dynamic form tag helper for ASP.NET Core. I heard back then that Angular 2 (now Angular 4) have a feature called reactive forms (now dynamic forms) which provides the capability to generate an html form with model binding and model validation from an annotated object.
Long story short, in order to play around with Angular 2 I had to get up to learn some TypeScript. I was glad to discover then a lot of interesting features such as static typing, class based object-orientation, for-each loops, interpolated strings and encapsulation capabilities to name a few.
Most of TypeScript goodness is already in ES6
I would like to highlight some of the features that were IMHO most needed.
Class based Object Orientation
It is possible to define a classes as in C#:
The previous class contains a constructor, fields , properties and methods.
You can see also that inheritance is now explicitly supported via the
This is a huge cognitive load saver especially for programmers coming from a C#, Java or C++ background.
for each loops
Consider the following example:
Yes, indeed you can say goodbye to most of the classic
for-of construct which eliminates the need to reason about counter initialization and boundary conditions.
map, reduce and filter
Since I discovered functional programming and with the availability of LINQ in C#, I became addicted to the
filter functions when working with lists, consider the following:
All of these functions are now available and as you can see in the previous example, ES6 supports the arrow function syntax for specifying anonymous functions (lambdas) in a more compact way. As in C# we can create expression lambdas as well as statement lambdas.
Small pattern matching support
Some pattern matching capability is now supported, consider the following:
In is now more easier to unpack complex structures such as objects and arrays with safe defaults.
I understand that this is far from the pattern matching capability of functional languages but it can be useful to have still.
Small Issue with the module system
By creating a file it is possible to define a module:
export keyword is used to mark a definition as public i.e. importable from another module. The
format function is not exported and is there for considered private to the module.
In the file in which we wish to use definitions from
mylib, we use the
import keyword to make the needed definitions available:
As you can see from the previous example, we can import specific definitions or we can use a wild card to import all the public definitions.
Unfortunately, at the time of this writing Node JS as well as all the modern browsers does not support the ES6 module syntax.
A similar approach is possible on the client side but it will require the use of require.js.
Checkout this article by James M Snell for more details.