स्थिर तरीके
स्थिर तरीकों . का उपयोग करना हम केवल एक वर्ग में तत्वों तक पहुँच सकते हैं लेकिन वस्तु में तत्वों को नहीं। स्थिर विधि . को कॉल करना संभव है केवल एक वर्ग के अंदर लेकिन किसी वस्तु में नहीं।
उदाहरण-1
निम्नलिखित उदाहरण में, स्थिर () विधि "कंपनी . वर्ग में शुरू की गई है "बल्कि एक वस्तु "myComp" में। इसलिए स्थिर () . में सामग्री विधि को आउटपुट में निष्पादित किया गया था।
<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>