How to access a JavaScript object using its own prototype?

Front End TechnologyJavascriptObject Oriented Programming

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
raja
Published on 26-Aug-2019 08:52:59
Advertisements