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

জাভাস্ক্রিপ্টে কনস্ট্রাক্টর মেথড কী?


কনস্ট্রাক্টর মেথড জাভাস্ক্রিপ্টে ক্লাসের ভেতরে অবজেক্ট তৈরি ও ইনিশিয়ালাইজ করতে ব্যবহৃত হয়। যদি কনস্ট্রাক্টর মেথড যোগ না করা হয়, তবে ডিফল্ট কনস্ট্রাক্টর ব্যবহার করা উচিত।

নোট − একটি ক্লাসে কনস্ট্রাক্টর মেথড শুধু একবার থাকতে পারে। একাধিক থাকলে এরর হবে।

উদাহরণ

নিচের কোডটি চালিয়ে কনস্ট্রাক্টর মেথড বাস্তবায়ন বুঝতে পারেন:

Live Demo

<html>    <body>       <script>           class Department {             constructor() {                 this.name = "Department";             }           }           class Employee extends Department {             constructor() {                 super();             }           }           class Company {}           Object.setPrototypeOf(Employee.prototype, Company.prototype);           document.write("<br>" + Object.getPrototypeOf(Employee.prototype) === Department.prototype);           let myInstance = new Employee();           document.write("<br>" + myInstance.name);       </script>    </body> </html>

  1. JavaScript alert() মেথড: ব্যবহার নির্দেশিকা

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

  3. জাভাস্ক্রিপ্ট কনস্ট্রাক্টরে প্রপার্টি এবং মেথড যোগ করার পদ্ধতি

  4. C#-এ মেথড হাইডিং (Method Hiding)