Javascript’s confusing “Prototypal inheritance”

Nikhil Nambiar
1 min readFeb 12, 2020

--

In Javascript, if you would like to extend an object, you can do it by using “__proto__” property on a js object.

let animal = { eats: true }

let rabbit = { walks: true, __proto__: animal }

rabbit.eats
// true
// Notice how the eats property from the animal object was inherited.

Writing new things to inherited properties

let animals = { eats: true, marine: true }

let rabbits = { jumps: true, __proto__: animals }

rabbits.marine = false;

console.log(rabbits)
// {jumps: true, marine: false}

Conclusion

You can inherit properties from parent objects. This only works with a single parent object.

--

--