Hi there, we’re Harisystems

"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".

For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.

1
1

Understanding JavaScript Inheritance: Examples and Usage

Inheritance is an essential concept in object-oriented programming that allows classes to inherit properties and methods from other classes. JavaScript, being a prototype-based language, supports inheritance through prototypal inheritance. In this article, we will explore JavaScript inheritance and provide examples of its usage.

1. Prototypal Inheritance

In JavaScript, objects can inherit properties and methods from other objects. Every object has a prototype, which is another object from which it inherits. When a property or method is accessed on an object, JavaScript looks for it on the object itself, and if not found, it looks up the prototype chain until it finds the property or method or reaches the end of the chain.

2. Object.create()

The Object.create() method allows you to create a new object with a specified prototype. Here's an example:

<script>
const personPrototype = {
  sayHello() {
    console.log("Hello!");
  }
};

const person = Object.create(personPrototype);
person.sayHello(); // Output: "Hello!"
</script>

In this example, we define a personPrototype object with a sayHello method. We then create a new object person using Object.create() and set its prototype to personPrototype. The person object inherits the sayHello method from its prototype.

3. Constructor Functions and the Prototype Chain

JavaScript constructor functions can be used to create objects with shared properties and methods. By convention, constructor functions start with an uppercase letter. Here's an example:

<script>
function Animal(name) {
  this.name = name;
}

Animal.prototype.eat = function() {
  console.log(this.name + " is eating.");
};

function Dog(name) {
  Animal.call(this, name);
}

Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;

Dog.prototype.bark = function() {
  console.log(this.name + " is barking.");
};

const dog = new Dog("Max");
dog.eat(); // Output: "Max is eating."
dog.bark(); // Output: "Max is barking."
</script>

In this example, we define an Animal constructor function with a name property and an eat method. We then define a Dog constructor function that calls the Animal constructor using Animal.call(this, name) to inherit the name property. We set the prototype of Dog to a new object created from Animal.prototype using Object.create() to establish the prototype chain. We also assign the constructor back to Dog.prototype.constructor since it is overwritten during the previous step. Finally, we define a bark method on the Dog.prototype. When we create an instance of Dog and call the methods, it correctly inherits the properties and methods from both Animal and Dog.

Conclusion

JavaScript inheritance allows for code reuse and promotes the creation of more maintainable and scalable applications. In this article, we explored the concept of prototypal inheritance and demonstrated its usage through examples. By understanding inheritance in JavaScript, you can leverage its power to create reusable and extensible code in your JavaScript projects.

4.5L

Learners

20+

Instructors

50+

Courses

6.0L

Course enrollments

4.5/5.0 5(Based on 4265 ratings)

Future Trending Courses

When selecting, a course, Here are a few areas that are expected to be in demand in the future:.

Beginner

The Python Course: Absolute Beginners for strong Fundamentals

By: Sekhar Metla
4.5 (13,245)
Intermediate

JavaScript Masterclass for Beginner to Expert: Bootcamp

By: Sekhar Metla
4.5 (9,300)
Intermediate

Python Coding Intermediate: OOPs, Classes, and Methods

By: Sekhar Metla
(11,145)
Intermediate

Microsoft: SQL Server Bootcamp 2023: Go from Zero to Hero

By: Sekhar Metla
4.5 (7,700)
Excel course

Future Learning for all

If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.

View Courses

Most Popular Course topics

These are the most popular course topics among Software Courses for learners