Table of contents
1.
Introduction 
2.
Class in Node.js using JavaScript prototype
2.1.
Code
3.
Class in Node.js using ES6
3.1.
Code
4.
Drawbacks of using ES6
5.
Strict Mode
6.
Frequently Asked Questions
6.1.
What is ES6?
6.2.
What is the difference between ES6 and JavaScript?
6.3.
What is a class?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

How to use Class in Node.js

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Classes are used in object-oriented programming (OOP) to describe one or more objects. It serves as a template for creating or instantiating specific objects within a program. A class in Node.js works similarly and is used to create objects. 

There are mainly two ways of writing a class in Node.js:

  1. Using JavaScript prototype
  2. Using ES6 (ECMAScript 6)


This blog will discuss how to use class in Node.js and code samples on creating and instantiating a class in Node.js.

Class in Node.js using JavaScript prototype

It is possible to write a class in Node.js using JavaScript. JavaScript has OOP support when we use prototypes.

Syntax:

className.prototype.methodName = function() {
}

Let's look at an example to understand how to write a class in Node.js using JavaScript.

Code

// Method which can be extended as a class
function Student() {
 this.id = "ID_01";
}

// Setting the method using prototype
Student.prototype.setName = function(name) {
 this.name = name;
};

// Setting the method using prototype
Student.prototype.sayHi = function() {
 console.log("Hi, " + this.name + ", your ID is " + this.id);
};

// Creating an object using the class Student
var newStudent = new Student();


// Calling the method to set the value
newStudent.setName("Sapna");

newStudent.sayHi();
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

Hi, Sapna, your ID is ID_01

 

In this case, we define a function, your "Class," and then define methods using the prototype.

Class in Node.js using ES6

The next method is using ES6. With the new Javascript standard ES6, we got a nice and clear syntax for creating classes. JavaScript classes are merely "syntactic sugar" over JavaScript's existing prototype-based inheritance to simplify the code for developers. Let's see how to perform the above operation using ES6.

Code

// Student class declaration
class Student {
 constructor() {
   this.id = "ID_01";
 }

 set name(name) {
   this._name = name;
 }

 get name() {
   return this._name;
 }

 sayHi() {
   console.log("Hi, " + this.name + ", your ID is " + this.id);
 }
}

var newStudent = new Student();
newStudent.name = "Sapna";

newStudent.sayHi();
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

Hi, Sapna, your ID is ID_01

As you can notice, the syntax in ES6 is easier to read and understand. Also, ES6 allows you to extend classes.

 

We can also declare the above class as an expression like shown below.

// Person class expression
let Person  = class {
}

Drawbacks of using ES6

One of the main drawbacks is that ES6 is not available on the client-side because most browsers don't support ES6. Due to this, classes on the server and client are different.
 

The other "drawback" is that you have to run your code in strict mode, forcing you to code more securely and with better performance.

Strict Mode

Strict mode enforces some strict principles for a JavaScript program. It means that the leverages given by JavaScript are not available while working in the strict mode. Using strict mode will make the code faster because the compiler can make better optimizations.

For example, if a variable is initialized with some value without declaring the variable, the JavaScript compiler will not throw an error and ignore it. But when strict mode is used, an error will be thrown.

 

Strict mode can be enabled in two ways:-

1. Starting the entire application in strict mode by starting node with

node —-use_strict webtential.js

2. Including the below code at the top of the file

"use strict";

Frequently Asked Questions

What is ES6?

ES6 or ECMAScript 2015 is the 6th version of the ECMAScript programming language standard. ECMAScript is the standardization of Javascript, which was released in 2015, and subsequently renamed ECMAScript 2015.

What is the difference between ES6 and JavaScript?

ES6 is the 6th version of ECMAScript, which has enhancements like class declarations, promises for asynchronous programming, lexical block scoping, destructuring patterns, proper tail calls, etc.

JavaScript is a prototype-based, multi-paradigm scripting language that is dynamic and supports object-oriented, imperative, and functional programming styles.

What is a class?

Class is a template used to create objects. They encapsulate data with code and work on that data. 

Conclusion

This blog covered the basic concepts of class in Node.js. We explored the two methods to use a class in Node.js: using JavaScript prototype and ES6. We also discussed the drawbacks of using ES6.

Don't stop here. Check out Top 10 NodeJS Frameworks In 2021. If you are new to Node.js, check out the blog Introduction to Node.js.

Recommended Reading:

Four Pillars of OOPS

Also, check out this amazing course on Full Stack Web Development in Node JS along with this Guided Path for Basics of JavaScript on Coding Ninjas Studio.

Happy Learning!!

Live masterclass