Prototypal inheritance is a way of creating objects and classes in JavaScript, and it differs from the classical inheritance model found in other languages such as Java. In prototypal inheritance, an object can inherit properties and methods from another object, rather than from a class.
Here is how it works:
- Every JavaScript object has a special property called "prototype", which is a reference to another object.
- When an object is created, it inherits properties and methods from its prototype object.
- An object can also have its own properties and methods, which are called "own properties" and "own methods".
- If a property or method is not found on the object itself, the JavaScript engine will look for it on the object's prototype, and if it's not found there, it will look in the prototype's prototype and so on. This is the chain of prototypes that forms the prototype chain.
For example:
let animal = { type: "unknown" }; let cat = { name: "Fluffy" }; cat.__proto__ = animal; console.log(cat.type); // "unknown" console.log(cat.name); // "Fluffy"
In this example, the cat object inherits the type property from the animal object through the prototype chain. It also has its own property name.
It's also possible to create new objects that inherit from existing objects using the Object.create() method. For example:
let newCat = Object.create(cat); console.log(newCat.name); // "Fluffy" console.log(newCat.type); // "unknown"
In this way, you can create an object hierarchy, with objects inheriting properties and methods from other objects. This approach allows for a more flexible and dynamic way of creating objects, and it's one of the core features of JavaScript.
Another way to create a new object that inherits from an existing object is by using the Object.create() method. This method creates a new object with a specified prototype, without having to directly manipulate the __proto__ property. Here is an example:
let animal = { type: "unknown" }; let cat = Object.create(animal); cat.name = "Fluffy"; console.log(cat.type); // "unknown" console.log(cat.name); // "Fluffy"
In this example, the cat object inherits from the animal object through the prototype chain, and it also has its own property name.
It's worth noting that JavaScript also has a built-in Object constructor, which is used to create objects that inherit from Object.prototype. For example:
let obj = new Object(); console.log(obj.__proto__ === Object.prototype); // true
In addition, JavaScript also has a feature called "prototype-based constructor functions" that simulates the behavior of classical inheritance and allows the creation of multiple instances with the same properties and methods. For example:
function Animal(type) { this.type = type; } Animal.prototype.speak = function() { console.log(`This ${this.type} makes a sound`); } let cat = new Animal("cat"); console.log(cat.type); // "cat" console.log(cat.speak()); // "This cat makes a sound"
In this case, Animal is a constructor function that when invoked with new keyword creates an object and assigns the Animal.prototype to its __proto__ property, allowing the new object to access the methods and properties defined on the prototype.
In summary, prototypal inheritance is a powerful feature of JavaScript that allows objects to inherit properties and methods from other objects, and it's a fundamental concept in understanding how JavaScript works.