class
একটি class হলো একটি ফাংশনের ধরন, কিন্তু 'function' কীওয়ার্ডের বদলে 'class' কীওয়ার্ড ব্যবহার করে এটি শুরু করা হয় এবং প্রপার্টিগুলো constructor() মেথডের ভিতরে এসাইন করা হয়। constructor() মেথডটি প্রতিবার ক্লাস অবজেক্ট ইনিশিয়ালাইজ হলে কল হয়।
উদাহরণ-১
নিচের উদাহরণে, 'company' নামে একটি class তৈরি করা হয়েছে এবং constructor() মেথডের ভিতরে কোম্পানির নাম এসাইন করা হয়েছে এবং আউটপুটে ফলাফল দেখানো হয়েছে।
<html> <body>
<p id="class"></p>
<script>
class Company {
constructor(branch) {
this.name = branch;
}
}
myComp = new Company("Tutorialspoint");
document.getElementById("class").innerHTML = myComp.name;
</script>
</body> </html>
আউটপুট
Tutorialspoint
উদাহরণ-২
<html> <body> <script>
class Company {
constructor(branch) {
this.name = branch;
}
}
myComp = new Company("Rk enterprises");
document.write(myComp.name);
</script> </body> </html>
আউটপুট
Rk enterprises