Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to access a JavaScript object using its own prototype?
JavaScript's Object.create() method creates a new object with the specified object as its prototype. This allows the new object to inherit properties from the existing object while maintaining a prototype chain.
Syntax
Object.create(prototypeObject);
This method takes an existing object and creates a new object that inherits properties from it through the prototype chain.
How It Works
When you create an object using Object.create(), the new object doesn't copy the properties directly. Instead, it creates a prototype link to the original object, allowing property inheritance.
<html>
<body>
<script>
var person = {
name: "Karthee",
profession: "Actor",
industry: "Tamil"
};
// Display original object properties
document.write("Original object properties:<br>");
document.write("Name: " + person.name + "<br>");
document.write("Profession: " + person.profession + "<br>");
document.write("Industry: " + person.industry + "<br><br>");
// Create prototype using Object.create()
var newper = Object.create(person);
// Override properties in the new object
newper.name = "Sachin";
newper.profession = "Cricketer";
newper.industry = "Sports";
document.write("New object properties:<br>");
document.write("Name: " + newper.name + "<br>");
document.write("Profession: " + newper.profession + "<br>");
document.write("Industry: " + newper.industry + "<br>");
</script>
</body>
</html>
Original object properties: Name: Karthee Profession: Actor Industry: Tamil New object properties: Name: Sachin Profession: Cricketer Industry: Sports
Prototype Chain Example
The following example demonstrates how the prototype chain works when properties are not overridden:
<html>
<body>
<script>
var vehicle = {
type: "Car",
wheels: 4,
getInfo: function() {
return this.type + " with " + this.wheels + " wheels";
}
};
var myCar = Object.create(vehicle);
myCar.brand = "Toyota"; // Add new property
// Accessing inherited properties
document.write("Type: " + myCar.type + "<br>");
document.write("Wheels: " + myCar.wheels + "<br>");
document.write("Brand: " + myCar.brand + "<br>");
document.write("Info: " + myCar.getInfo() + "<br>");
</script>
</body>
</html>
Type: Car Wheels: 4 Brand: Toyota Info: Car with 4 wheels
Key Points
-
Object.create()establishes a prototype relationship, not property copying - The new object can override inherited properties by setting its own values
- Properties not found in the new object are looked up in the prototype chain
- Methods from the prototype object are also inherited and accessible
Conclusion
Object.create() is a powerful way to create objects with prototype inheritance in JavaScript. It allows you to build object hierarchies while maintaining clean prototype chains for property lookup and method inheritance.
