Lots of people don’t know it, but you can use and extend real classes in Node.js already. There’s a few drawbacks, but once you learn about them, they’re really not drawbacks but postive things, that will make your code faster and better.

Doesn’t Javascript already have OOP?

Sure, Javascript does of course already have OOP, when you use

className.prototype.methodName = function() { }

etc., but with the new Javascript standard ES6, we got a real nice and clear syntax for creating classes.

Let’s take a simple “Person” example.

This is how you would probably do it right now, by defining a function, which is your “Class” and then define methods by using prototype. If you want to do something special when assigning the name, you would probably make a setName function for it.

And here is how we’d do the same thing in ES6:

Now this is not magically better, but in my opinion the syntax is definately easier to read, and you can use getters/setters if you want (if you want a setName method and do it the old way, you can do that too of course).

Also, ES6 makes you able to extend classes just by writing class Employee extends Person, with all the implications you’d imagine. This is really great, compared to the old way, where you basically have to go through hoops to do the same.

What are the drawbacks?

As I mentioned there are drawbacks. The main one being, that this is not available clientside, because most browsers doesn’t support ES6 now. Which will make your classes on the server and client a bit different. This is not really something I notice a lot myself when I program, my brain easily goes into “clientside” mode with the differences that are there already.

The other “drawback” is that you have to run your code in strict mode, but this isn’t really a drawback at all, since strict mode will force you to code more securely and with better performance.

What is strict mode, and why should I use it?

Strict mode changes previously accepted “bad syntax” into real errors.

It might be a bit annoying in the start if you were used to slacking and writing loose code, or maybe not even knowing that your code wasn’t tight, but trust me, it’s all for the better. Your code will even be faster, because the compiler can make better optimizations.

I won’t mention everything that strict mode does, but I encourage you to take a glance at the best reference I found, which (believe it or not) is Microsofts Javascript documentation.

The thing that will probably affect the most people is that you can’t use undefined variables.

But as I mentioned, this shouldn’t be happening anyway. People will ridicule you and call you bad names if they find out you code like this, so really it’s for the best that you find out that you’re following bad practices now instead of later.

So, how do we enable this in Node.js?

If you tried to use the above example right now, you’d get an error. Node.js will only use ES6 if you start it in strict mode.

You can enable strict mode either by starting your entire application in strict mode, by starting node with

node —-use_strict webtential.js

or by enabling it in a single file, by inserting

"use strict";

at the top of your file.

I hope you could use this, and I hope you have an awesome and nice day.

Written by Martin Mouritzen.