Welcome to the fascinating world of prototypes in JavaScript! Prototypes are the cornerstone of object-oriented programming in this language. They act as hidden blueprints that define the properties and methods that objects inherit. This chapter delves into the core concepts, practical applications, and advanced aspects of prototypes, empowering you to craft well-structured and reusable objects in your JavaScript code.
Object itself) has a hidden property called [[Prototype]] (often referred to as __proto__ in developer tools). This property points to the object’s prototype, which is another object that serves as its blueprint.
const person = {};
console.log(person.hasOwnProperty('toString')); // Output: false (inherited)
person.toString method on person, we can still call it (person.toString()). This is because person inherits the toString method from its prototype (likely the built-in Object.prototype).hasOwnProperty checks if a property is directly defined on the object itself, not inherited. Here, it returns false for toString[[Prototype]] property. This process continues until the property is found or the end of the chain (usually Object.prototype) is reached.
function Animal(species) {
this.species = species;
}
Animal.prototype.makeSound = function() {
console.log("Generic animal sound");
};
const dog = new Animal("Dog");
dog.makeSound(); // Output: Generic animal sound
Animal constructor with a species property and a makeSound method on its prototype.dog object using new Animal, it inherits the makeSound method from the Animal.prototype.makeSound isn’t directly defined on dog, it’s accessible because JavaScript follows the prototype chain to find it.
function Animal(species) {
this.species = species;
}
Animal.prototype.walk = function() {
console.log(`${this.species} is walking.`);
};
const dog = new Animal("Dog");
const cat = new Animal("Cat");
dog.walk(); // Output: Dog is walking.
cat.walk(); // Output: Cat is walking.
Animal constructor and add a walk method to its prototype.dog and cat objects (created using Animal) can inherit and use the walk method, even though it’s not defined directly on them.
function Animal(species) {
this.species = species;
}
Animal.prototype.makeSound = function() {
console.log("Generic animal sound");
};
function Dog(species) {
this.species = species;
}
Dog.prototype.makeSound = function() {
console.log("Woof!");
};
const dog = new Dog("Dog");
dog.makeSound(); // Output: Woof!
Animal constructor with a makeSound method in its prototype.Dog constructor that inherits from Animal.Dog also defines its own makeSound method.dog object, it uses the makeSound method defined in the Dog.prototype, overriding the inherited one from Animal.prototype.Object.create() method allows you to create a new object using an existing object as the prototype. This provides more control over inheritance compared to using constructors directly.
const animalProto = {
makeSound() {
console.log("Generic animal sound");
}
};
const dog = Object.create(animalProto);
dog.species = "Dog";
dog.makeSound(); // Output: Generic animal sound
animalProto that serves as the prototype.Object.create(animalProto) to create a new object dog that inherits from animalProto.species to dog.dog.makeSound() is called, it uses the inherited method from animalProto.Object.getPrototypeOf(object) method returns the object’s immediate prototype in the chain. This can be helpful for debugging and understanding inheritance relationships.
function Animal(species) {
this.species = species;
}
const dog = new Animal("Dog");
const animalPrototype = Object.getPrototypeOf(dog);
console.log(animalPrototype === Animal.prototype); // Output: true
Animal constructor.dog object using Animal.Object.getPrototypeOf(dog) returns the prototype object (Animal.prototype) that dog inherits from.Animal.prototype to verify they are the same object.Prototypes are a fundamental concept in JavaScript's object-oriented paradigm. By understanding how they establish inheritance and how to manipulate them, you can create well-structured and reusable objects. Remember, prototype chains can become complex in large applications, so plan your inheritance structure carefully! Happy coding !❤️
