A simple Javascript class example with static method for Node.js

CK CK 3788 
Created at  
662 0 0

Here's a simple example of a JavaScript class with a static method, suitable for use in Node.js:

// ExampleClass.js
class ExampleClass {
  // Constructor method
  constructor(name) {
    this.name = name;
  }

  // Instance method
  greet() {
    return `Hello, ${this.name}!`;
  }

  // Static method
  static sayHelloToEveryone() {
    return 'Hello, everyone!';
  }
}

// Export the class so it can be used in other files
module.exports = ExampleClass;

 

How to Use the Class in Node.js:

You can use this class in another file by requiring it and creating an instance of the class.

// app.js
const ExampleClass = require('./ExampleClass');

// Creating an instance of the class
const instance = new ExampleClass('Alice');

// Calling the instance method
console.log(instance.greet()); // Output: Hello, Alice!

// Calling the static method
console.log(ExampleClass.sayHelloToEveryone()); // Output: Hello, everyone!

 

Explanation:

  1. Constructor Method: The constructor method is called when you create a new instance of the class. It initializes the instance with properties, such as name.
  2. Instance Method: The greet method is an instance method that operates on a specific instance of the class. It uses the this keyword to access the instance's properties.
  3. Static Method: The sayHelloToEveryone method is a static method, meaning it belongs to the class itself, not to any particular instance. You can call it without creating an instance of the class.

This is a basic example, but it shows the fundamental structure of a class with both instance and static methods in Node.js.

Tags Constructor Method Instance Method Javascript Class Simple Javascript Class Example Static Class Method Static Javascript Class Method Static Method Facebook X
Comments 0
Similar posts
  1. Using a module in the external class itself without creating an instance in Node.js
    350
  2. JavaScript Algorithms
    722
  1. Get client IP address and user agent in Node.js
    1,531
  2. forEach method in JavaScript when working with arrays
    667
  3. CRC32B function code in Javascript
    1,845
  4. Node.js-based Web Server supporting multiple path
    604
  5. Setting a Common Path for require() in Node.js
    1,007
  6. Import all JS files in a specific folder for Node.js programming
    1,733
  7. A simple example to access a PostgreSQL database in Node.js
    1,101
  8. Node.js code example to access Google BigQuery
    1,183
  9. Investigation on package.js for Node.js source code
    824
  10. Using MySQL in Node.js
    485
  11. A Node.js script that checks if a module is installed before requiring it
    774
  12. Worker Threads and Mutual Exclusion in Node.js
    466
  13. Node.js external modules
    434
  14. Multi-threaded Web Server based in Node.js
    866
  15. A basic HTTP server based in Node.js
    544
Recently updated
  1. Google OAuth Token Expiration: Causes and Solutions
    31
  2. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    57
  3. Why SPF and DKIM Are Essential for Linux Email Servers?
    51
  4. How do I create animated background for Google Meet?
    23,255
  5. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    273
  6. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    104
  7. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    170
  8. Array of arguments passed to script in PHP
    99
  9. Invalid Auction Storms in Ad Tech
    108
  10. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    209
  11. The Two Clocks of Ad Server Architecture
    78
  12. Frequency Cap Architecture Overview
    527
  13. Collecting Proof-of-Play from 100,000 Screens
    153
  14. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    303
  15. What is RedisVL?
    117
  16. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    247
  17. TPU & GPU Cluster Management Tools
    258
  18. The Concept of Scaffolding in AI Agents
    245
  19. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    598
  20. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    269
  21. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    355
  22. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    207
  23. Machine Learning: Training vs. Ranking
    230
  24. The Continuous Learning Machine: Designing a Loop That Actually Turns
    229
  25. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    204
  26. Designing a Data Curation System Around a Vector Database
    230
  27. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    263
  28. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    674
  29. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    201
  30. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    233