JavaScript / JavaScript Object-Oriented Programming (OOP)

Exploring Prototype and Inheritance

This tutorial explores the principles of prototypes and inheritance in JavaScript OOP. It includes practical examples and exercises.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Introduces OOP principles in JavaScript, including classes, objects, and inheritance.

1. Introduction

1.1 Brief explanation of the tutorial's goal

In this tutorial, we aim to explore and understand the concepts of prototypes and inheritance in JavaScript Object Oriented Programming (OOP).

1.2 What the user will learn

By the end of this tutorial, you will learn:

  • What prototypes are in JavaScript
  • How JavaScript uses prototypes for inheritance
  • Practical examples of prototypes and inheritance

1.3 Prerequisites

Basic understanding of JavaScript is required. Familiarity with Object Oriented Programming would be useful but not necessary.

2. Step-by-Step Guide

2.1 Prototypes in JavaScript

In JavaScript, every object has a prototype. The prototype is itself an object, and all objects inherit their properties and methods from their prototype.

let animal = {
  eats: true
};

let rabbit = Object.create(animal);

console.log(rabbit.eats); // true

In the above example, rabbit inherits the eats property from the animal object.

2.2 JavaScript Prototype Inheritance

In JavaScript, objects can inherit from other objects. This is known as prototype inheritance.

let animal = {
  eats: true,
  walk() {
    console.log("Animal walk");
  }
};

let rabbit = Object.create(animal);

rabbit.walk(); // Animal walk

In this example, rabbit inherits the walk method from the animal object.

3. Code Examples

Example 1

function Rabbit(name) {
  this.name = name;
}

Rabbit.prototype.eat = function() {
  console.log(this.name + ' eats.');
}

let rabbit1 = new Rabbit('White Rabbit');

rabbit1.eat(); // White Rabbit eats

In this example, we add a method eat to the prototype of Rabbit. All instances of Rabbit will have this method.

Example 2

function Animal(name) {
  this.name = name;
}

Animal.prototype.walk = function() {
  console.log(this.name + ' walks.');
}

function Rabbit(name) {
  this.name = name;
}

Rabbit.prototype = Object.create(Animal.prototype);

let rabbit1 = new Rabbit('Brown Rabbit');

rabbit1.walk(); // Brown Rabbit walks

In this example, Rabbit inherits from Animal by setting its prototype to an instance of Animal.

4. Summary

In this tutorial, we have covered:

  • The concept of prototypes in JavaScript
  • How JavaScript uses prototypes for inheritance
  • Examples demonstrating prototypes and inheritance

Next, you could explore more about JavaScript OOP, including classes and constructors. Here are some helpful resources:

5. Practice Exercises

Exercise 1

Create an object cat that inherits from animal and add a method meow to cat.

let animal = {
  eats: true
};

let cat = // Your code here

cat.meow(); // should log "Cat meows"

Exercise 2

Modify the Rabbit function to accept name and color properties. Make sure color is available to all instances and methods of Rabbit.

function Rabbit(name) {
  this.name = name;
}

// Your code here

let rabbit1 = new Rabbit('Grey Rabbit', 'grey');

console.log(rabbit1.color); // should log "grey"

Solutions

Solution for Exercise 1

let animal = {
  eats: true
};

let cat = Object.create(animal);

cat.meow = function() {
  console.log("Cat meows");
};

cat.meow(); // Cat meows

Solution for Exercise 2

function Rabbit(name, color) {
  this.name = name;
  this.color = color;
}

let rabbit1 = new Rabbit('Grey Rabbit', 'grey');

console.log(rabbit1.color); // grey

In the above solution, we added a color property to the Rabbit constructor function. Now, color is available to all instances and methods of Rabbit.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Random Password Generator

Create secure, complex passwords with custom length and character options.

Use tool

Interest/EMI Calculator

Calculate interest and EMI for loans and investments.

Use tool

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

Fake User Profile Generator

Generate fake user profiles with names, emails, and more.

Use tool

Word to PDF Converter

Easily convert Word documents to PDFs.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help