Share
Facebook
Twitter
LinkedIn
Whatsapp
Instagram
Messenger

জাভাস্ক্রিপ্ট স্তাটিক মেতদ


জাভাস্ক্রিপ্ট এর স্তাটিক মেতদ অল Static methods in JavaScript are a way to define methods on a class that are called on the class itself, rather than on instances of the class. This can be useful for utility functions, factory methods, or other operations that don't require access to instance-specific data.


স্তাটিক মেতদ কি?

স্ট্যাটিক পদ্ধতিগুলি ক্লাস এর মাজেই সংজ্ঞায়িত করা হয়, এর উদাওরনের উপর নয় এই ক্লাস গুলোকে তাদের ক্লাসের কোন একটি উদাহরণের পরিবর্তে সরাসরি ক্লাস এর নামেই ডাকা হয়।

জাভাস্ক্রিপ্টে, আমরা কোন একটি ক্লাস এর মধ্যে " static " কীওয়ার্ড ব্যবহার করে একটি স্ট্যাটিক মেতদ সংজ্ঞায়িত করতে পারি। এখানে নিচে এর একটি সিনট্যাক্স দেকুন।

উদাহরণ


class MyClass {
  static myStaticMethod() {
    console.log('This is a static method.');
  }
}

// Call the static method directly on the class
MyClass.myStaticMethod(); // Output: This is a static method.


কোড এডিটর


Key Points Called on the Class: Static methods are invoked on the class itself, not on instances of the class. Cannot Access Instance Properties: Static methods don't have access to instance properties or methods. They only have access to static properties and other static methods. Use Cases: Static methods are commonly used for utility functions, creating instances in a specific way (factory methods), and other functionality that doesn’t depend on individual instances.


ইউটিলিটি ফাংশন

স্ট্যাটিক মেতদ বাবয়ার করে একটি ক্লাস তৈরি করতে পারি যা আমাদের জন্য একটি ইউটিলিটি ফাংশন তৈরি করে। নিচে এরকম একটি উয়াওরন দেকুন জেকানে একটি ইউটিলিটি ফাংশন তৈরি করা অয়েচে।

উদাহরণ


class MathUtils {
  // Static method to calculate the factorial of a number
  static factorial(n) {
    if (n <= 1) return 1;
    return n * MathUtils.factorial(n - 1);
  }
}

// Use the static method directly from the class
console.log(MathUtils.factorial(5)); // Output: 120

কোড এডিটর



Factory Method

Static methods can also be used as factory methods to create instances of the class:

উদাহরণ


class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  static createAdult(name) {
    return new Person(name, 18);
  }
}

// Create an instance using the static method
const john = Person.createAdult('John');
console.log(john); // Output: Person { name: 'John', age: 18 }


কোড এডিটর



Static Methods from Other Static Methods

একটি স্ট্যাটিক মেতদ ভিন্ন কোন স্ট্যাটিক মেতদ কে একই ক্লাসে কল করতে পারে। নিচে একটি স্ট্যাটিক মেতদ তেকে অন্য একটি স্ট্যাটিক মেতদ এর একটি উদাউরন দেকুন।

উদাহরণ


class Utility {
  static double(x) {
    return x * 2;
  }

  static tripleAndDouble(x) {
    const triple = x * 3;
    return Utility.double(triple);
  }
}

console.log(Utility.tripleAndDouble(5)); // Output: 30

কোড এডিটর


নোট - No this Binding: Inside a static method, this refers to the class itself, not an instance. For instance methods, this refers to the instance.

নোট - Inheritance: Static methods are not inherited by subclasses unless explicitly called. Subclasses can override static methods.