स्टैटिक मेथड्स
स्टैटिक मेथड्स का उपयोग करके, हम केवल क्लास में मौजूद एलिमेंट्स को एक्सेस कर सकते हैं लेकिन ऑब्जेक्ट में मौजूद एलिमेंट्स को नहीं। स्टैटिक मेथड को केवल क्लास के अंदर ही कॉल करना संभव है लेकिन ऑब्जेक्ट में नहीं।
उदाहरण-1
निम्नलिखित उदाहरण में, static() मेथड क्लास "Company" में इनिशिएट किया गया है न कि ऑब्जेक्ट "myComp" में। इसलिए static() मेथड की सामग्री आउटपुट में निष्पादित हुई।
<html> <body> <p id="method"></p> <script> class Company { constructor(branch) { this.name = branch; } static comp() { return "Tutorix is the best e-learning platform" } } myComp = new Company("Tesla"); document.getElementById("method").innerHTML = Company.comp(); </script> </body> </html>
आउटपुट
Tutorix is the best e-learning platform
उदाहरण-2
निम्नलिखित उदाहरण में, क्लास के बजाय ऑब्जेक्ट को कॉल किया गया है इसलिए कोई आउटपुट निष्पादित नहीं होगा। यदि हम ब्राउज़र कंसोल खोलते हैं तो हम एक त्रुटि देख सकते हैं जिसमें कहा गया है कि "myComp.comp()" एक फंक्शन नहीं है।
<html> <body> <p id="method"></p> <script> class Company { constructor(branch) { this.name = branch; } static comp() { return "Tutorix is the best e-learning platform" } } myComp = new Company("Tesla"); document.getElementById("method").innerHTML = myComp.comp(); </script> </body> </html>