Prototypal Inheritance — JavaScript

Kumar Himanshu
2 min readFeb 4, 2024

Prototypal inheritance helps you reuse one object's property and method inside another.

Reuse != Copy/Reimplement its method  

[[Prototype]] — Every object in Javascript has a hidden property called [[Prototype]] that is either ‘null’ or references another Object.

Prototypal inheritance — When we read a property from ‘object’ and if it’s missing, then Javascript takes it from its ‘prototype object’. This is called prototypal inheritance/ prototypal chaining.

let developer = {
code: true,
isCoding(){
console.log("on Work")
}
}

let reactDeveloper = {
reactSkills: true,
__proto__: developer
}

reactDeveloper.reactSkills // true
reactDeveloper.code // true, because of prototypal inheritance, it tries to find
// 'code' property inside reactDeveloper. As it's not there,
// JS follows [[Prototype]] reference and find it in 'developer'
reactDeveloper.isCoding() // "on Work"

Limitations:
1. The reference can’t go in a circle, JS will throw an error
2. The value of __proto__ can either be object or null. other types are ignored.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Kumar Himanshu
Kumar Himanshu

Written by Kumar Himanshu

A front-end developer passionately working to make it BIGGG

No responses yet

Write a response