कक्षा
एक वर्ग एक प्रकार का फ़ंक्शन है, लेकिन 'फ़ंक्शन . कीवर्ड का उपयोग करने के बजाय ', कीवर्ड 'वर्ग ' इसे आरंभ करने के लिए प्रयोग किया जाता है, और गुणों को एक constructor() . के अंदर असाइन किया जाता है तरीका। निर्माता () हर बार क्लास ऑब्जेक्ट को इनिशियलाइज़ करने के लिए मेथड को कॉल किया जाता है।
उदाहरण-1
निम्नलिखित उदाहरण में, एक वर्ग 'कंपनी . कहा जाता है ' बनाया गया है और एक कन्स्ट्रक्टर () . के अंदर है विधि कंपनी का नाम असाइन किया जाता है और परिणाम को आउटपुट में प्रदर्शित करता है।
<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
उदाहरण-2
<html>
<body>
<script>
class Company {
constructor(branch) {
this.name = branch;
}
}
myComp = new Company("Rk enterprises");
document.write(myComp.name);
</script>
</body>
</html> आउटपुट
Rk enterprises