JavaScript Class কি?

JavaScript Classes – সম্পূর্ণ বাংলা টিউটোরিয়াল

JavaScript Classes হচ্ছে একটি modern syntax যা JavaScript এর object-oriented programming (OOP) কে আরও পরিষ্কার এবং readable করে তোলে। এটি ES6 (ECMAScript 2015) থেকে এসেছে। এই টিউটোরিয়ালে আমরা class, constructor, method, inheritance এবং static method নিয়ে বিস্তারিত জানবো।

JavaScript Class কি?

একটি class হল object তৈরি করার একটি ব্লুপ্রিন্ট। আপনি যদি একই রকমের একাধিক object তৈরি করতে চান, তবে class ব্যবহার করে আপনি কোড পুনঃব্যবহার করতে পারবেন।

Class Declaration

উদাহরণ:

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

greet() {
return `Hello, my name is ${this.name}`;
}
}

উপরের উদাহরণে আমরা একটি Person নামের ক্লাস তৈরি করেছি, যার মধ্যে একটি constructor এবং একটি method আছে।

Constructor কি?

Constructor হল একটি বিশেষ method যা যখন নতুন object তৈরি হয় তখন automatic ভাবে চালু হয়। এটি object এর initial property সেট করতে ব্যবহৃত হয়।

Constructor এর উদাহরণ:

const p1 = new Person("Rahim", 28);
console.log(p1.greet()); // Hello, my name is Rahim

Methods কি?

Method হল class এর ভিতরে থাকা function। এটি object দ্বারা call করা যায় এবং সেই object এর data নিয়ে কাজ করে।

Class Inheritance বা Extends

Inheritance ব্যবহার করে আপনি একটি class কে আরেকটি class থেকে extend করতে পারেন, যাতে parent class এর property এবং method child class এ পাওয়া যায়।

উদাহরণ:

class Student extends Person {
constructor(name, age, grade) {
super(name, age);
this.grade = grade;
}

study() {
return `${this.name} is studying in grade ${this.grade}`;
}
}

এখানে Student class Person কে extend করেছে এবং super() keyword দিয়ে parent class এর constructor call করা হয়েছে।

Static Methods

Static methods class এর instance এর সাথে নয়, বরং class নিজেই call করে।

Static Method:

class MathUtil {
static add(a, b) {
return a + b;
}
}

console.log(MathUtil.add(5, 3)); // 8

Difference between Class and Function Constructor

Constructor Function ES6 Class
Prototype-based Syntactic sugar over prototype
Function keyword ব্যবহার করতে হয় class keyword ব্যবহার করা হয়
Less readable More structured and readable

নিয়মিত ব্যবহৃত Class উদাহরণ

  • Component তৈরি (React JS)
  • Data Models
  • Utility বা Helper Functions
  • API structure / SDK Libraries

উপসংহার

JavaScript Class হচ্ছে একটি শক্তিশালী টুল, যা object তৈরি ও ব্যবস্থাপনায় কার্যকরী। এটি traditional function constructor এর তুলনায় অনেক cleaner ও modern। Object-oriented concept বুঝে এর যথাযথ ব্যবহার আপনার JS স্কিলকে এক ধাপ এগিয়ে নেবে।


 

Leave a Reply