কম্পিউটার টিউটোরিয়াল

জাভাস্ক্রিপ্টে মেথড শেয়ার করা

মেথডগুলো অবজেক্টের প্রোটোটাইপ প্রোপার্টির সাথে जोড়ে শেয়ার করা যায়। এই মেথডগুলো অবজেক্টের সব ইনস্ট্যান্সের মধ্যে শেয়ার থাকবে।

জাভাস্ক্রিপ্টে মেথড শেয়ার করার জন্য নিচের কোডটি দেখুন।

উদাহরণ

<!DOCTYPE html> <html lang="bn"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>মেথড শেয়ারিং উদাহরণ</title> <style>    body {       font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;    }    .result {       font-size: 20px;       font-weight: 500;       color: blueviolet;    } </style> </head> <body> <h1>জাভাস্ক্রিপ্টে শেয়ার্ড মেথড</h1> <div class="result"></div> <button class="Btn">এখানে ক্লিক করুন</button> <h3>উপরোক্ত বাটনে ক্লিক করুন student1 এবং student2 অবজেক্টের displayInfo মেথড কল করতে</h3> <script>    let resEle = document.querySelector(".result");    function Student(name, age, standard) {       this.name = name;       this.age = age;       this.standard = standard;    }    Student.prototype.displayInfo = function () {       return `${this.name} বয়স ${this.age} : শ্রেণি :${this.standard} <br>`;    };    let student1 = new Student("Rohan", 17, 12);    let student2 = new Student("Shawn", 16, 11);    document.querySelector(".Btn").addEventListener("click", () => {       resEle.innerHTML = student1.displayInfo();       resEle.innerHTML += student2.displayInfo();    }); </script> </body> </html>

আউটপুট

প্রাথমিক অবস্থা:

জাভাস্ক্রিপ্টে মেথড শেয়ার করা

'এখানে ক্লিক করুন' বাটনে ক্লিক করার পর:

জাভাস্ক্রিপ্টে মেথড শেয়ার করা

  1. জাভাস্ক্রিপ্টে মেথড বরো করা (call, apply, bind)

  2. জাভাস্ক্রিপ্ট প্রোটোটাইপে মেথড যোগ করা

  3. জাভাস্ক্রিপ্ট অবজেক্টের অ্যারেতে অ্যারে মেথডগুলো ব্যবহার করা

  4. জাভাস্ক্রিপ্টে ক্লাসে স্ট্যাটিক মেথড অ্যাসাইন করা