क्लास (Class)
एक क्लास एक प्रकार का फंक्शन है, लेकिन 'function' कीवर्ड का उपयोग करने के बजाय, इसे इनिशिएट करने के लिए 'class' कीवर्ड का उपयोग किया जाता है, और प्रॉपर्टीज को constructor() मेथड के अंदर असाइन किया जाता है। constructor() मेथड हर बार क्लास ऑब्जेक्ट इनिशियलाइज़ होने पर कॉल होता है।
उदाहरण-1
निम्नलिखित उदाहरण में, क्लास 'company' बनाई गई है और 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
उदाहरण-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