ABHAY TEWATIA’s Post

🚀 JavaScript Simplified Series — Day 34 Objects are powerful… But what if you could **share properties and methods between objects automatically?** 🤔 Without copying code again and again 😵 This is where **Prototypes** come in 🔥 --- ## 🔥 The Problem ```javascript id="pt1" let user1 = { name: "Abhay", greet: function() { console.log("Hello") } } let user2 = { name: "Rahul", greet: function() { console.log("Hello") } } ``` 👉 Same function repeated ❌ 👉 Code duplication ❌ --- ## 🔥 Solution → Prototype JavaScript objects have a hidden property: 👉 **[[Prototype]]** Through this, objects can **inherit properties** --- ## 🔹 Example ```javascript id="pt2" function User(name) { this.name = name } User.prototype.greet = function() { console.log("Hello " + this.name) } let u1 = new User("Abhay") let u2 = new User("Rahul") u1.greet() u2.greet() ``` 👉 Output: Hello Abhay Hello Rahul --- ## 🔍 What’s happening? 👉 `greet()` is not inside object 👉 It is shared via prototype 📌 Memory efficient + reusable --- ## 🔥 Real Life Example Think of a **template 🧾** 👉 Same format 👉 Different data Like: Form template → reused for all users --- ## 🔥 Prototype Chain If property not found: 👉 Object → Prototype → Next Prototype → null --- ## 🔥 Simple Summary Prototype → shared properties Avoid duplication Enable inheritance --- ### 💡 Programming Rule **Don’t repeat logic. Share it using prototypes.** --- If you want to learn JavaScript in a **simple and practical way**, you can follow these YouTube channels: • Rohit NegiHitesh Choudhary Choudhary (Chai aur Code) --- 📌 Series Progress Day 1 → What is JavaScript Day 2 → Variables & Data Types Day 3 → Type Conversion & Operators Day 4 → Truthy & Falsy + Comparison Operators Day 5 → If Else + Switch + Ternary Day 6 → Loops Day 7 → Break + Continue + Nested Loops Day 8 → Functions Basics Day 9 → Arrow + Default + Rest Parameters Day 10 → Callback & Higher Order Functions Day 11 → Arrays Basics Day 12 → Array Methods Day 13 → Array Iteration Day 14 → Advanced Array Methods Day 15 → Objects Basics Day 16 → Object Methods + this Day 17 → Object Destructuring Day 18 → Spread & Rest Day 19 → Advanced Objects Day 20 → DOM Introduction Day 21 → DOM Selectors Day 22 → DOM Manipulation Day 23 → Events Day 24 → Event Bubbling Day 25 → Event Delegation Day 26 → Async JavaScript Day 27 → Promises Day 28 → Async / Await Day 29 → Fetch API Day 30 → Event Loop Day 31 → Scope Day 32 → Hoisting Day 33 → Closures Day 34 → Prototypes Day 35 → Classes (Next Post) --- Follow for more 🚀 #JavaScriptSimplified #javascript #webdevelopment #coding #programming #learninpublic #100DaysOfCode #frontenddevelopment #devcommunity #codingjourney #softwaredeveloper #techcommunity #dailylearning #codeeveryday

To view or add a comment, sign in

Explore content categories