
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use Class in Node.js
Introduction
Node.js is trending as an environment where different efficient and logical applications are executed at the moment. One of them is extending modern JS syntax, for example, classes, which makes OOP in Node.js more possible to use and improve. Scroll down to learn what a class is in Node.js, how one can be defined, how methods can/must be added, subclass/superclass in Node.js, and some uses of classes in Node.js.
What is a Class in Node.js?
A class is a blueprint of objects that have certain characteristics and behaviors or actions. Classes were introduced in ECMAScript 2015 ( ES6) as a way of getting a more organized way of writing Object Oriented Programming code in JavaScript.
Why Use Classes in Node.js?
Classes help in:
- Code organization: The Group related functionality for the reason of better modularity should be maintained.
- Reusability: Use subtyping to reuse of existing classes to the maximum.
- Readability: Syntax is neater than in older proto-typing approaches based on the prototype objects.
- Scalability: Especially used to solve large scale and complicated problems.
Basic Syntax of a Class
Here's a simple example of a class in Node.js:
class Person { constructor(name, age) { this.name = name; this.age = age; } greet() { return `Hello, my name is ${this.name} and I am ${this.age} years old.`; } } // Usage const person1 = new Person('Alice', 30); console.log(person1.greet());
Output
Hello, my name is Alice and I am 30 years old.
Creating and Using Methods
Classes allow the defining of methods within them. These methods are invoked using instances of the class:
class Calculator { add(a, b) { return a + b; } subtract(a, b) { return a - b; } } // Usage const calc = new Calculator(); console.log(calc.add(5, 3)); // Output: 8 console.log(calc.subtract(5, 3)); // Output: 2
Output
8 2
Inheritance in Classes
Inheritance allows a class to reuse the logic of another class using the extends
keyword:
class Animal { constructor(name) { this.name = name; } speak() { return `${this.name} makes a noise.`; } } class Dog extends Animal { speak() { return `${this.name} barks.`; } } // Usage const dog = new Dog('Buddy'); console.log(dog.speak()); // Output: Buddy barks.
Output
Buddy barks.
Using Classes with Modules in Node.js
Node.js involves a modular concept referring to module.exports and require . Here's how you can use classes with modules:
File: math.js
class MathOperations { multiply(a, b) { return a * b; } } module.exports = MathOperations;
File: app.js
const MathOperations = require('./math'); const math = new MathOperations(); console.log(math.multiply(3, 4)); // Output: 12
Practical Example: Building a User Model
Classes are very helpful in use cases such as the user management. Here's an example:
class User { constructor(username, email) { this.username = username; this.email = email; } getDetails() { return `Username: ${this.username}, Email: ${this.email}`; } } // Usage const user = new User('john_doe', '[email protected]'); console.log(user.getDetails());
Output
Username: john_doe, Email: [email protected]
Conclusion
Classes in Node.js makes coding neat, well-organized, and easy to follow and read. This article offered simple instructions on creating, working with and extending classes which were supported by examples. But, with the help of this, you can easily understand how to structure your Node.js applications.