Classes and patterns in Node.js
This week we had the awesome Node.js 6.0.0 release! With the 6.0.0 release Node.js has 93% of the ES6 features implemented. In this short post we will look at few particularly interesting features including “classes in JavaScript”, while this aspect is a little bit controversial with the purists it nonetheless allows us to write some clean code.
If you want to install Node.js 6 easily I would recommend n. With n setup, installing it was as easy as running (on a Debian based distro) -
We can now have a simple npm setup where we will demostrate some new Node.js features like classes, destructuring and default parameters and using them we will create the Singleton and Factory pattern. Here is the code -
lib/singleton.js
test/singleton_test.js
lib/factory.js
test/factory_test.js
As evident with the code, Node.js just keeps getting better and better. One thing to note is ES6 modules/import/export are still not there because that could lead to a tons of npm packages being outdated / needing updates. So for now Node.js will stick to CommonJS module system. Hope this was useful, have a great weekend!