20. Prototypes & Classes
Prototypes & Inheritance
Unlike languages like Java, JavaScript is not traditionally class-based. It is Prototype-based. When ES6 introduced the class keyword, it was just syntactical "sugar" hiding the underlying Prototype Chain.
๐งฌ The Prototype Chain
When you call a method on an array (like .push()), the array itself doesn't contain that code. JavaScript looks at the array, doesn't find the method, and searches up the "Prototype Chain" to the master Array.prototype object, which holds all the shared methods.
๐๏ธ Constructor Functions (The Old Way)
Before ES6, we built object templates using standard functions and the new keyword.
๐๏ธ ES6 Classes (The Modern Way)
The modern syntax provides a much cleaner, Java-like structure for creating objects and dealing with inheritance.
Database wiped by AdminPrince
Knowledge Check
Ready to test your understanding of 20. Prototypes & Classes?