What Is Prototypal Inheritance

Every object you create inherit s properties from the prototypes all the way up the prototype chain.

In our example, circle1, inherits the methods get Name() and get Circumf erence() from its prototype (because we specifically added them to the prototype), and circle1 inherits the method t oString() from the Object .prototype.

We call this prot ot ypal inherit ance, which means that if you try to access a property in an object, and that property doesn’t exist in the object itself, JavaScript looks up the prototype chain to try to find that property.

Most objects either have one or two prototypes. An object will have the prototype, Object .prototype, if it is an object literal, or if it has been created using new Object ().

An object will have two prototypes if it is created using a constructor function like Circle(), in which case the first prototype up the chain is the Circle. prototype and the second is one more step up the chain, Object .prototype.

Some objects have a longer chain, but that’s fairly rare. Let’s come back to inst anceof for a moment. Earlier we talked about inst anceof as an operator to determine which type of object you have.

How uses Prototypal Inheritance

We use it in the example for this lesson to figure out whether shape is a circle or a square:

if (shape instanceof Circle) {
classes += "circle";
width = shape.radius;
} else if (shape instanceof Square) {
classes += "square";
width = shape.size;
}

nst anceof checks the prototype chain of shape to see if there is a Circle.prot ot ype object or a Square.prot ot ype object.

In our example, the shape was constructed by either the Circle() or Square() constructor functions, so our shape will a have Circle. prototype prototype, or a Square. prototype
prototype, and so we’ll find one of these prototypes in the chain.

Object .prototype is also in the prototype chain, so we could write this:

>circle1 instanceof Circle
true
>circle1 instanceof Object
true
>square instanceof Square
true
>square instanceof Object
true

circle1 is an instance of both a Circle and an Object! The same is true with square; square is an instance of both a Square and an Object. That makes sense, because all objects are instances of Object, after all.

However, the extra object in the prototype chain gives you more information about the kind of object you’re dealing with.

In this case, it tels you whether the object is a circle or a square. If you have experience using an object-oriented language like Java or C++ or C#, prototypal inheritance
probably seems quite strange to you.

It certainly is different from classical inheritance, where you create objects from classes, and create an inheritance hierarchy by extending other objects.

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Prototypal Inheritance in javascript

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password