JavaScript Prototypes Explained

🚀 Day 11/100 – Understanding Prototypes in JavaScript Today I deep dived into one of the most fundamental concepts in JavaScript: Prototypes. 🧠 Problem: How does JavaScript enable inheritance between objects? ✅ Example: function Person(name) { this.name = name; } Person.prototype.greet = function () { console.log(`Hello, my name is ${this.name}`); }; const user1 = new Person("Bunty"); user1.greet(); // Output: Hello, my name is Bunty 💡 What’s Happening Here? Every JavaScript function has a prototype property. Objects created using new inherit from that prototype. Methods defined on the prototype are shared across all instances. This improves memory efficiency. 📌 Why Not Define Method Inside Constructor? If we write: function Person(name) { this.name = name; this.greet = function () { console.log(`Hello, my name is ${this.name}`); }; } Now every instance gets its own copy of greet() ❌ Which increases memory usage. Prototype keeps it optimized ✅ 🧠 Key Learnings: JavaScript uses prototypal inheritance (not classical inheritance). The __proto__ links objects to their prototype. Method lookup happens through the prototype chain. Core JS concepts like Array, Object, Function are built using prototypes. Understanding prototypes makes closures, inheritance, and even React internals easier to grasp. I’m currently open to Frontend Developer opportunities (React / Next.js) and available for immediate joining. 📩 Email: bantykumar13365@gmail.com 📱 Mobile: 7417401815 If you're hiring or know someone who is, feel free to connect. #OpenToWork #FrontendDeveloper #JavaScript #Prototypes #ReactJS #ImmediateJoiner #100DaysOfCode

To view or add a comment, sign in

Explore content categories