- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to access a JavaScript object using its own prototype?
We can able to access the existing object by creating its own prototype using a javascript method called "Object.create()". Using this method we can inherit the properties from the existing properties to the newly created prototype. Let's discuss it in a nutshell.
syntax
Object.create(existing obj);
This method takes the existing object and creates its own prototype so that the properties will be inherited from the existing object to the newly created prototype.
Example
In the following example, Initially, an object named "person" is created and the using "Object.create" its own prototype is created and assigned to a variable "newper". Later on, using the prototype the objects of the existing object have been changed and the new properties were displayed as shown in the output.
<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); /// creating prototype 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>
Output
Karthee Actor Tamil Using a prototype the properties of the existing object have been changed to the following sachin crickter sports
Advertisements