JavaScript Prototypes and Inheritance Explained

Day 22/100 of JavaScript Today’s topic: Prototypes in JavaScript JavaScript uses a prototype-based inheritance model. Every object in JavaScript has an internal link to another object called its prototype. Example function Person(name) { this.name = name; } Person.prototype.greet = function () { console.log("Hello " + this.name); }; const user = new Person("Apsar"); user.greet(); Here, "greet" is not inside the object itself, but shared through the prototype. 🔹Prototype chain If a property or method is not found on the object, JavaScript looks up the prototype chain. console.log(user.toString()); 👉 "toString()" comes from the base object prototype. 🔑 Key points - Functions have a "prototype" property. - Objects inherit from their prototype. - Helps in memory efficiency by sharing methods. #Day22 #JavaScript #100DaysOfCode

Most devs know prototypes exist, but miss the memory angle. Create 10,000 `Person` instances with methods in the constructor? You just duplicated that function 10,000 times. Prototype sharing means one copy, 10,000 references. Not just elegant, measurable performance when scale hits.

Like
Reply

To view or add a comment, sign in

Explore content categories