logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Understanding Prototypal Inheritance in Vanilla JavaScript

author
Generated by
Abhishek Goyan

22/10/2024

JavaScript

Sign in to read full article

When we talk about JavaScript, one of the most fascinating concepts that come up is prototypal inheritance. Unlike classical inheritance found in languages like Java or C++, JavaScript employs prototypal inheritance, which allows for a more dynamic and flexible way of creating objects and sharing properties. Let’s break this concept down step-by-step.

What is Prototypal Inheritance?

In JavaScript, every object has a prototype. A prototype is simply another object from which the current object can inherit properties and methods. This form of inheritance allows for shared state and behavior, leading to cleaner and more efficient code.

The Prototype Chain

The real magic happens through something called the prototype chain. When you try to access a property of an object, JavaScript first looks at the object's own properties. If it doesn’t find it, it will then look up the chain to the object's prototype, and then to the prototype’s prototype, and so on, until it either finds the property or reaches the top of the chain—Object.prototype.

Here is a simple visual representation of the prototype chain:

Object A
  └── Prototype A (Object.prototype)
  └── Prototype B (Another Object or prototype chain)

Creating Objects Using Prototypal Inheritance

Let’s see how to create objects that inherit properties from a prototype. You can create an object using a constructor function and then add properties to its prototype.

Example 1: Constructor Function and Prototypes

function Animal(name) { this.name = name; } Animal.prototype.speak = function() { console.log(`${this.name} makes a noise.`); }; const dog = new Animal('Dog'); dog.speak(); // "Dog makes a noise."

In this example, we define a constructor function Animal that takes a name as an argument. By adding a method speak to Animal.prototype, all instances of Animal (like dog) can use this method without each instance having its own copy.

Example 2: Inheritance Between Two Constructors

To demonstrate inheritance further, let’s create a Dog class that inherits from Animal.

function Dog(name, breed) { Animal.call(this, name); // Call the parent constructor this.breed = breed; } // Inherit the methods from Animal Dog.prototype = Object.create(Animal.prototype); Dog.prototype.constructor = Dog; Dog.prototype.speak = function() { console.log(`${this.name}, the ${this.breed}, barks.`); }; const myDog = new Dog('Rex', 'German Shepherd'); myDog.speak(); // "Rex, the German Shepherd, barks."

Here’s what’s happening in the above code:

  1. Calling the Parent Constructor: Inside the Dog constructor, we call Animal.call(this, name) to ensure that the name property is initialized correctly.

  2. Inheriting Methods: We assign Dog.prototype to an object created from Animal.prototype using Object.create(). This establishes the prototype chain.

  3. Overriding Methods: We override the speak method in the Dog constructor. This method will use name and breed to provide specific output.

Example 3: Object Literal with Prototype

JavaScript also allows you to create objects using object literals and specify the prototype through the Object.setPrototypeOf() method.

const animal = { speak() { console.log(`${this.name} makes a noise.`); } }; const cat = { name: 'Cat', }; Object.setPrototypeOf(cat, animal); cat.speak(); // "Cat makes a noise."

Here, we create an animal object with a speak method. The cat object is created with a name property and uses Object.setPrototypeOf() to set animal as its prototype. Now, cat can utilize the speak method from its prototype.

Understanding the Downside

One potential downside of using prototypal inheritance is that it can sometimes lead to unintentional mutations. If you modify a prototype, all objects that inherit from it will reflect that change. This can lead to bugs if you're not careful.

Conclusion

Prototypal inheritance is a cornerstone of how JavaScript handles objects. Its dynamic nature allows developers to create flexible and scalable applications. By understanding how prototypes work, you can harness their full potential in your own projects, making your code more efficient and easier to maintain. Embracing prototypal inheritance paves the way for cleaner object structures and a keen foundation for more advanced JavaScript patterns like composition and mixins.

Popular Tags

JavaScriptPrototypal InheritanceVanilla JS

Share now!

Like & Bookmark!

Related Collections

  • JavaScript Mastery: From Basics to Advanced Techniques

    15/10/2024 | VanillaJS

  • JavaScript Interview Mastery: 20 Essential Concepts

    22/10/2024 | VanillaJS

  • JavaScript Coding Challenges for Interviews

    14/09/2024 | VanillaJS

Related Articles

  • Understanding and Implementing Currying in JavaScript Functions

    14/09/2024 | VanillaJS

  • An In-depth Look at Event Loop in JavaScript

    21/07/2024 | VanillaJS

  • Implementing Pub/Sub Pattern in JavaScript

    12/09/2024 | VanillaJS

  • Creating a Throttle Function in JavaScript

    14/09/2024 | VanillaJS

  • Create a Custom Event Emitter in JavaScript

    14/09/2024 | VanillaJS

  • Understanding JavaScript Symbols

    22/10/2024 | VanillaJS

  • Understanding the JavaScript Event Loop

    22/10/2024 | VanillaJS

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design