Prototypes are the mechanism by which JavaScript objects inherit features from one another. In simple terms, every object in javascript has a hidden property [[Protoype]]which is nothing but a reference to another object.

But the mechanism is not as simple as it seems. When accessing the properties of the object, it shows distinct and surprising behavior in different situations as we will explore below.

The required property is not present in the [[Prototype]] chain of the object.

We will examine the cases where the desired property is not present higher up the chain of the object.

  1. If the object already has a property present on it, then the assignment will be as simple as changing the value of the existing property of the object.
const obj = {
  bar:'😃'
}

obj.bar = '😇' // simply changing the property

2. If the desired property is not present directly on the object, then the [[Prototype]] chain of the object is traversed. If the property is not found anywhere in the [[Prototype]] chain, then it is added directly to the object with the specified value.

const obj = {}

obj.bar = '😀' // property is added directly to the object

#programming #coding #javascript #software-development #technology #prototypes

Understanding JavaScript — Prototypes
1.25 GEEK