हम मौजूदा ऑब्जेक्ट को एक्सेस कर सकते हैं जावास्क्रिप्ट मेथड "Object.create()" का उपयोग करके इसका अपना प्रोटोटाइप बनाकर। इस मेथड का उपयोग करके हम मौजूदा प्रॉपर्टीज से नए बने प्रोटोटाइप में प्रॉपर्टीज इनहेरिट कर सकते हैं। आइए इसे संक्षेप में समझते हैं।
सिंटैक्स
Object.create(existing obj);
यह मेथड मौजूदा ऑब्जेक्ट लेता है और इसका अपना प्रोटोटाइप बनाता है ताकि प्रॉपर्टीज मौजूदा ऑब्जेक्ट से नए बनाए गए प्रोटोटाइप में इनहेरिट होंगी।
उदाहरण
निम्नलिखित उदाहरण में, प्रारंभ में "person" नामक एक ऑब्जेक्ट बनाया गया है और "Object.create" का उपयोग करके इसका अपना प्रोटोटाइप बनाया गया है और "newper" नामक वेरिएबल को असाइन किया गया है। बाद में, प्रोटोटाइप का उपयोग करके मौजूदा ऑब्जेक्ट के ऑब्जेक्ट्स को बदल दिया गया है और नई प्रॉपर्टीज आउटपुट में दिखाए गए अनुसार प्रदर्शित की गई हैं।
<html> <body> <script> var person = { name: "Karthee", profession : "Actor", industry: "Tamil" }; document.write(person.name); document.write("</br>"); document.write(person.profession); document.write("</br>"); document.write(person.industry); document.write("</br>"); document.write("Using a prototype the properties of the existing object have been changed to the following"); document.write("</br>"); var newper = Object.create(person); /// प्रोटोटाइप बनाना newper.name = "sachin"; newper.profession = "crickter"; newper.industry = "sports"; document.write(newper.name); document.write("</br>"); document.write(newper.profession); document.write("</br>"); document.write(newper.industry); </script> </body> </html>
आउटपुट
Karthee Actor Tamil Using a prototype the properties of the existing object have been changed to the following sachin crickter sports