प्रॉपर्टीज को ऑब्जेक्ट की प्रोटोटाइप प्रॉपर्टी से जोड़कर शेयर किया जा सकता है। ये प्रॉपर्टीज ऑब्जेक्ट के सभी इंस्टेंसेज के बीच शेयर की जाएंगी।
जावास्क्रिप्ट में शेयर्ड प्रॉपर्टीज का कोड निम्नलिखित है -
उदाहरण
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 20px; font-weight: 500; color: blueviolet; } </style> </head> <body> <h1>जावास्क्रिप्ट में शेयर्ड प्रॉपर्टीज</h1> <div class="result"></div> <button class="Btn">यहाँ क्लिक करें</button> <h3>student1 और student2 ऑब्जेक्ट्स की प्रॉपर्टीज देखने के लिए ऊपर दिए गए बटन पर क्लिक करें</h3> <script> let resEle = document.querySelector(".result"); function Student(name, age) { this.name = name; this.age = age; } Student.prototype.school = "St Marks School"; let student1 = new Student("Rohan", 17); let student2 = new Student("Shawn", 16); document.querySelector(".Btn").addEventListener("click", () => { resEle.innerHTML = `${student1.name} age ${student1.age} : school :${student1.school} <br>`; resEle.innerHTML += `${student2.name} age ${student2.age} : school :${student2.school}`; }); </script> </body> </html>
आउटपुट

'यहाँ क्लिक करें' बटन पर क्लिक करने पर -
