Prototype Chain
PronunciationPROH-tuh-type CHAYN
Definition
In JavaScript every object can link to another object, its prototype. When you read a property that the object doesn't have, JavaScript follows these links up the prototype chain until it finds it or reaches the end.
Where you hear it
In JavaScript interviews, explanations of class and extends, and debugging "why does this object have that method?".
Examples
arr.mapisn't on the array itself; it's found onArray.prototypeup the chain.A JavaScript
classis syntax over prototype links.Setting a property on the prototype makes it visible to every object created from it.
Common mistake
Modifying built-in prototypes such as Array.prototype. It affects every array in the program and breaks libraries.
Don't confuse with
Class-based inheritance in Python or Java, where objects come from a class blueprint. JavaScript objects link directly to other objects.
Say it at work
- In a meeting or chat
Look at the prototype chain to see where it's defined.
- In an email or ticket
hasOwnPropertyignores the chain.
Keep exploring
More in Programming Fundamentals