Nidhi Jagga’s Post

𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭'𝐬 "𝐇𝐢𝐝𝐝𝐞𝐧 𝐂𝐡𝐚𝐢𝐧": 𝐔𝐧𝐥𝐨𝐜𝐤𝐢𝐧𝐠 𝐏𝐫𝐨𝐭𝐨𝐭𝐲𝐩𝐞𝐬 & 𝐈𝐧𝐡𝐞𝐫𝐢𝐭𝐚𝐧𝐜𝐞 🔗🧬 Ever wonder why you can do `"hello".toUpperCase()` even though strings are just simple text (primitives)? Or why JavaScript Classes feel... distinct from other languages? It is all because of 𝐏𝐫𝐨𝐭𝐨𝐭𝐲𝐩𝐚𝐥 𝐈𝐧𝐡𝐞𝐫𝐢𝐭𝐚𝐧𝐜𝐞, the engine running under the hood. 1️⃣𝐏𝐫𝐢𝐦𝐢𝐭𝐢𝐯𝐞𝐬 𝐚𝐫𝐞 "𝐒𝐞𝐜𝐫𝐞𝐭" 𝐎𝐛𝐣𝐞𝐜𝐭𝐬 🕵️♂️ Strings, Numbers, and Booleans are primitives. But when you access a property like `.length`, JS temporarily wraps them in an object (e.g., `new String("Nidhi")`) so they can borrow methods from their prototype. 2️⃣𝐓𝐡𝐞 𝐏𝐫𝐨𝐭𝐨𝐭𝐲𝐩𝐞 𝐂𝐡𝐚𝐢𝐧 ⛓️ When you ask for a property, JS checks the object. If it's not there, it travels up the `__proto__` chain to the parent, then the grandparent, all the way until it hits `null`. • `str.__proto__` ➔ `String` • `String.__proto__` ➔ `Object` • `Object.__proto__` ➔ `null` (The End) 3️⃣𝐂𝐥𝐚𝐬𝐬𝐞𝐬 𝐚𝐫𝐞 "𝐒𝐮𝐠𝐚𝐫" 🍬 The `class` keyword (ES6) is just a friendly syntax wrapper. Internally, `class Student` still creates a function, and `new Student()` still links `__proto__` to `Student.prototype`. 4️⃣𝐓𝐡𝐞 𝐆𝐨𝐥𝐝𝐞𝐧 𝐑𝐮𝐥𝐞: • `prototype`: Belongs to the 𝐂𝐥𝐚𝐬𝐬/𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧 (The Blueprint). • `__proto__`: Belongs to the 𝐈𝐧𝐬𝐭𝐚𝐧𝐜𝐞 (The Link back to the blueprint). Check out the visual guide below to see the chain in action! 👇 How long did it take you to fully grasp the difference between `prototype` and `__proto__`? #JavaScript #WebDevelopment #CodingDeepDive #Prototypes #SoftwareEngineering #Frontend

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories