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.

 Live Demo

<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

Updated on: 26-Aug-2019

292 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements