Open In App

How to use Class in Node ?

Improve
Improve
Like Article
Like
Save
Share
Report

In Node, classes function as templates for creating objects in object-oriented programming, encapsulating both data and behavior. They provide a structured and reusable approach to defining and instantiating objects within a JavaScript program.

We will discuss the following two approaches to define a class in Node:

Using JavaScript prototype:

In Node, JavaScript embraces Object-Oriented Programming (OOP) through prototypes, offering a way to shape and utilize classes.

Unlike certain programming languages with explicit class structures, JavaScript doesn’t have traditional classes. Instead, it relies on prototypes for creating and handling objects. A prototype serves as a model object, and other objects inherit properties from it. When you employ the class syntax in JavaScript, you’re essentially crafting a constructor function and specifying methods on its prototype.

Syntax:

className.prototype.methodName = function() {
}

Example: An example to see how to create a class in Nodejs using JavaScript.

Javascript




// Define a class using a constructor function
function UniversityStudent() {
    this.studentID = "UNI_ID_001";
}
 
// Add a method to set the student's name
UniversityStudent.prototype.setStudentName =
    function (studentName) {
        this.name = studentName;
    };
 
// Add a method to greet the student
UniversityStudent.prototype.greetStudent =
    function () {
        console.log(
            "Hello, " + this.name +
            "! Your university ID is " + this.studentID
        );
    };
 
// Create an object using the UniversityStudent class
var newUniversityStudent = new UniversityStudent();
 
// Call the method to set the student's name
newUniversityStudent.setStudentName("Ashish");
 
// Call the method to greet the student
newUniversityStudent.greetStudent();


Output:

Hello, Ashish! Your university ID is UNI_ID_001

Using ES6:

The alternative approach involves utilizing ES6. With the introduction of the ES6 JavaScript standard, a concise and straightforward syntax for constructing classes emerged. JavaScript classes in ES6 act as a form of “syntactic sugar,” streamlining the process of prototype-based inheritance for developers. Let’s explore how to achieve the same operation using ES6.

Example: An example to see how to create a class in Nodejs using JavaScript.

Javascript




// UniversityStudent class declaration
class UniversityStudent {
    constructor() {
        this.studentID = "UNI_ID_001";
    }
 
    set studentName(studentName) {
        this._studentName = studentName;
    }
 
    get studentName() {
        return this._studentName;
    }
 
    greetStudent() {
        console.log(
            "Hello, " + this.studentName +
            "! Your university ID is " + this.studentID);
    }
}
 
var newUniversityStudent = new UniversityStudent();
newUniversityStudent.studentName = "Ashish";
 
newUniversityStudent.greetStudent();


Output:

Hello, Ashish! Your university ID is UNI_ID_001

The ES6 syntax is more readable and comprehensible. Additionally, ES6 facilitates class extension, enabling you to build upon and enhance existing classes.

Drawbacks of using ES6

A notable limitation is that ES6 is often unsupported on the client-side since many browsers lack ES6 support. Consequently, classes implemented on the server and client sides may differ. Another consideration is the necessity of running your code in strict mode, a requirement that promotes more secure and performance-oriented coding practices.



Last Updated : 18 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads