Prototypal Inheritance in JavaScript
Michael Mitrakos
3 min read
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach…
Prototypal Inheritance in JavaScript
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach engineers to have solid foundations that will help them get ahead in their career. I also build awesome products for digital nomads — check it out!
JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. Check out the ebook now!
Prototypical inheritance is a fundamental concept in the JavaScript programming language that allows developers to create new objects that inherit properties and methods from existing objects.
In JavaScript, every object has a prototype, which is an object that serves as the template for the object. When an object is created, it automatically inherits properties and methods from its prototype. This allows developers to create objects that share common characteristics and behaviors, without having to write the same code over and over again.
To understand how prototypical inheritance works, it is important to first understand the concept of the prototype chain. Every object in JavaScript has a prototype property, which points to another object. This prototype object, in turn, has its own prototype, and so on, forming a chain of prototypes.
When a property or method is accessed on an object, JavaScript will first look for it on the object itself. If it is not found, it will then search for it on the object’s prototype, and then on the prototype’s prototype, and so on, until it reaches the end of the prototype chain.
Here is an example of prototypical inheritance in action:
// Define a base object with a property and a method
console.log(`My name is ${this.name}`); ```javascript
}
// Create a new object that inherits from the base object
// The derived object has access to the sayName method inherited from the base object
derivedObject.sayName(); // Output: "My name is Derived object"
// We can also access the base object's properties and methods directly
Prototypical inheritance is a powerful tool that allows developers to create complex object hierarchies and reuse code in a flexible and efficient way. It is an important concept to understand when working with JavaScript and other object-oriented programming languages.
#### JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. [Check out the ebook now](https://www.mitrakos.com/ebook)!
I founded [Higglo Digital](https://higglo.io) and we can help your business crush the web game with an award-winning website and cutting-edge digital strategy. If you want to see a *beautifully designed website*, [check us out](https://higglo.io).
I also created [Wanderlust Extension](http://www.wanderlustapp.io/) to discover the most beautiful places across the world with highly curated content. Check it out!