DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • 5 Best Node.js Practices to Develop Scalable and Robust Applications
  • Concurrency and Parallelism in Node.js for Scalable Apps
  • Streamline Microservices Development With Dapr and Amazon EKS

Trending

  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  • Concourse CI/CD Pipeline: Webhook Triggers
  1. DZone
  2. Coding
  3. JavaScript
  4. How to Create & Instantiate a Class in Node.js

How to Create & Instantiate a Class in Node.js

This article offers tips and code samples on creating and instantiating a class in Node.js. Check it out and learn more.

By 
Ajitesh Kumar user avatar
Ajitesh Kumar
·
Feb. 08, 16 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
66.1K Views

Join the DZone community and get the full member experience.

Join For Free

This article offers tips and code samples on creating and instantiating a class in Node.js. Please feel free to comment/suggest if I failed to mention one or more important points. 

How to Create a Class

//  This is a Constructor function taking age and passport 
//  as the paramaters
function Person(age, passport) {
  this.age = age;
  this.passport = passport;
}
// Sets the age
// 
Person.prototype.setAge = function(age) {
    this.age = age;
};
// Checks whether the person is Adult based on the age
// 
Person.prototype.isAdult = function() {
    return this.age >= 18? true: false;
};
// Checks whether the person can have bank accounts 
// based on whether he/she is an adult
// 
Person.prototype.canHaveBankAccounts = function() {
    return this.isAdult()?true:false;
};
// Sets the passport status of the person
// 
Person.prototype.passportStatus = function(status) {
    this.passport = status;
};
// Checks whether the person has a passport
// 
Person.prototype.hasPassport = function() {
    return this.passport;
};
//  Sets the Person object to module.exports
// 
module.exports = Person;

How to Instantiate a Class

The following code also demonstrates the following:

  • Check the parameters passed to "node" command: The function used is "process.argv". The first element is a "node" command. The second element at index 1 is the file name. The third element is the first parameter passed to the "node" command.
  • Exit the execution: The function used is "process.exit()".
var Person = require("./Person.js");
// Checks whether minimum of 3 parameters have been entered
// 
if(process.argv.length <= 2) {
  console.log("You must pass the age of the person.");
  // Exists 
  process.exit();
}
// Creates the person using the constructor function
// 
var person = new Person(1, false);

console.log("Person is an adult is: " + person.isAdult());
console.log("Person can have bank account is: " + person.canHaveBankAccounts());
// Sets the age of the person
// 
person.setAge(process.argv[2]);

console.log("Person is an adult is: " + person.isAdult());
console.log("Person can have bank account is: " + person.canHaveBankAccounts());
Node.js

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • 5 Best Node.js Practices to Develop Scalable and Robust Applications
  • Concurrency and Parallelism in Node.js for Scalable Apps
  • Streamline Microservices Development With Dapr and Amazon EKS

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: