🍏 JS Daily Bite #9 — Understanding Function Prototypes Every function in JavaScript has a special property called prototype — the foundation of how inheritance and object construction work in the language. 🔍 The Function Prototype Property All functions (except arrow functions) have a default prototype property Arrow functions ❌ do not have a prototype The prototype object includes a constructor property that points back to the function Each prototype object’s [[Prototype]] links up to Object.prototype 🧱 Adding Properties to Prototypes You can attach properties or methods to a function’s prototype to make them automatically available to all instances created with that function. This is a memory-efficient way to share behavior across instances! ⚡️ 🧩 Creating Instances with new When you use the new operator: A new object is created Its [[Prototype]] is set to the function’s prototype property Instance-specific properties are initialized Shared methods are inherited through the prototype chain 🧭 How Property Lookup Works When accessing a property: JavaScript checks the object itself If not found, it climbs the prototype chain ([[Prototype]]) The search continues until Object.prototype If still not found, the result is undefined 🧠 Key Takeaways Instance properties override prototype properties Prototype properties are shared across all instances The chain always ends at Object.prototype.[[Prototype]] === null Mastering this mechanism is essential for deep JavaScript understanding 💪 🔜 Next in the Series → Comparing Ways to Create and Mutate the Prototype Chain #JavaScript #WebDevelopment #Programming #TechEducation #LearnToCode #SoftwareDevelopment #JSInsights #CodeLearning #Frontend #JSFundamentals #Prototypes #JSDailyBite
Reyhaneh Khoshghadam’s Post
More Relevant Posts
-
🍏 JS Daily Bite #10 — JavaScript Prototype Chains: A Full Comparison Master JavaScript's prototype-based inheritance system by exploring ways to create and manipulate prototype chains, their trade-offs, and best practices. 🏗️ Methods for Creating Prototype Chains: 1️⃣ Object Literal Syntax with __proto__ ✅ Standardized, optimized, more performant than Object.create() ✅ Ergonomic for declaring properties at creation ⚠️ Fails silently when pointing to non-objects 2️⃣ Constructor Functions ✅ Fast, standard, JIT-optimizable ⚠️ Methods are enumerable by default, inconsistent with class syntax ⚠️ Error-prone for longer inheritance chains 3️⃣ Object.create() ✅ Direct prototype setting at creation ✅ Can create objects with null prototype ⚠️ Verbose, error-prone, potentially slower than literals 4️⃣ Classes (ES6+) ✅ Ideal for complex inheritance with readability and maintainability ✅ Supports private elements ⚠️ Less optimized than traditional constructors 🔧 Mutating Existing Prototype Chains: 1️⃣ Object.setPrototypeOf() ✅ Modify the prototype of existing objects ⚠️ Can disrupt engine optimizations 💡 Best practice: set prototype during object creation 2️⃣ The __proto__ Accessor ⚠️ Fails silently with non-objects ⚠️ Non-standard and deprecated 💡 Recommendation: use Object.setPrototypeOf() instead ⚡ Performance Considerations: 1️⃣ Prototype Chain Lookup Costs Deep chains slow property access Non-existent properties traverse the entire chain All enumerable properties in the chain are iterated 2️⃣ Checking Own Properties Use hasOwnProperty() or Object.hasOwn() Don’t rely solely on undefined checks 🔜 Next in the Series: Enumerability and ownership of properties #JavaScript #JSDailyBite #WebDevelopment #Programming #LearnToCode #Prototypes #Inheritance #SoftwareEngineering #FrontendDevelopment #JSFundamentals #TechEducation
To view or add a comment, sign in
-
🔥 JavaScript: When “prototype” is NOT an object 👀 We all learn early — > “Every function in JavaScript has a prototype property that is an object.” But JavaScript loves exceptions 😏 Here are some surprising cases where prototype is not an object 👇 RegExp.prototype // → /(?:)/ Array.prototype // → [] Function.prototype // → ƒ () {} Number.prototype // → Number {} String.prototype // → String {} Boolean.prototype // → Boolean {} Symbol.prototype // → Symbol {} BigInt.prototype // → BigInt {} 💡 Notice something? Some of these are functions, arrays, or even regular expressions! That’s because their prototypes are actual instances of their types, not plain objects — so that all instances inherit their core methods directly. For example: Array.prototype.push === [].push // true ✅ RegExp.prototype.test === /abc/.test // true ✅ So next time you assume prototype is always {}, remember — JS is full of living examples 😉 --- 💬 Have you ever found a weird prototype behavior while debugging? Drop it in the comments — let’s break more JS myths together! ⚙️ #JavaScript #WebDevelopment #LearningEveryday #Frontend
To view or add a comment, sign in
-
🍏 JS Daily Bite #7 🧬 JavaScript Inheritance: Understanding the Prototype Chain JavaScript's approach to inheritance is unique — and understanding it is key to mastering the language. 🚀 What is the Prototype Chain? JavaScript objects are dynamic collections of properties ("own properties"). But here's where it gets interesting: every object also has a link to a prototype object. When you try to access a property, JavaScript doesn't just look at the object itself — it searches up the prototype chain until it either finds the property or reaches the end of the chain. 🧩 🔑 Key Concepts to Know: [[Prototype]] is the internal link to an object's prototype, accessible via Object.getPrototypeOf() and Object.setPrototypeOf(). Don’t confuse obj.__proto__ with func.prototype — the latter specifies what prototype will be assigned to instances created by a constructor function. In object literals, you can use { __proto__: c } to set the prototype directly. 🧠 The “Methods” Twist: JavaScript doesn’t have methods in the traditional class-based sense. Functions are just properties that can be inherited like any other. And here's a critical detail: when an inherited function executes, this points to the inheriting object — not the prototype where the function is defined. ⚡ 💡 Why This Matters: Understanding prototypes is essential for working with JavaScript's object model, debugging inheritance issues, and leveraging modern class syntax (which is really just syntactic sugar over prototypes). 👉 Next up: Constructors — how JavaScript creates and links objects during instantiation! #JavaScript #JSDailyBite #WebDevelopment #Programming #FrontendDevelopment #SoftwareEngineering #LearnToCode #TechEducation #CodeNewbie #Developers #100DaysOfCode
To view or add a comment, sign in
-
How JS Converts [] + {} to [object Object] —🤯 JavaScript can be weirdly magical sometimes. Ever tried this in your console? 👇 [] + {} // Output: "[object Object]" At first glance, it looks confusing — why does an empty array and an empty object become a string? Let’s decode the magic 🪄 1️⃣ + Operator in JS The + operator doesn’t just add numbers — it can also concatenate strings. 2️⃣ Type Conversion Happens! [] (empty array) → when converted to string becomes "" (empty string). {} (empty object) → when converted to string becomes "[object Object]". 3️⃣ Final Expression So JS actually does: "" + "[object Object]" → "[object Object]" ✅ Bonus twist: Try reversing it: {} + [] Now it gives 0 because {} is treated as an empty block,not an object. 🤯 JavaScript — where logic meets magic ✨ 🔹 Follow Prashansa Sinha for more fun JS mysteries and simple explanations 👩💻 #JavaScript #WebDevelopment #Coding #Frontend #JS #LearnToCode #Programming #TechCommunity #Developers #CodeNewbie #WebDev
To view or add a comment, sign in
-
#7: Control Flow & Iterations in JavaScript! 🚀 Just wrapped up another core chapter of my JS journey — understanding how decisions and loops drive program logic. Here’s what I explored today: 🔹 Control Flow & Conditional Statements ✅ if, else if, else — mastering decision-making ✅ Comparison operators (===, !==, >, <, etc.) ✅ Logical operators (&&, ||) for combined conditions ✅ Shorthand execution methods for cleaner code 🔹 Switch Statements ✅ Cleaner multi-condition handling ✅ Importance of break and default 🔹 Truthy & Falsy Values ❌ Falsy: false, 0, "", null, undefined, NaN ✅ Truthy surprises: "0", "false", " ", [], {}, function(){} 💡 Learned: Check arrays with .length & objects using Object.keys() 🔹 Advanced Operators ✅ Nullish Coalescing (??) → safer than || for null/undefined ✅ Ternary Operator → condition ? true : false (clean & compact) 🔹 Loops & Iterations ✅ for loops (including nested + multiplication tables) ✅ while & do-while (executes at least once!) ✅ break to exit | continue to skip 📍 Key Insights: ✔ Use === for strict equality ✔ [] is truthy → always check .length ✔ ?? > || when dealing with null/undefined ✔ Ternaries keep logic short but readable The deeper I go into JavaScript, the more powerful and enjoyable it becomes! 💪 💬 Let’s discuss — which control flow concept took you the longest to grasp? 👇 #JavaScript #Programming #WebDevelopment #CodingJourney #LearnToCode #SoftwareDevelopment #TechSkills
To view or add a comment, sign in
-
🚀 Day 30/50 – Function Currying in JavaScript Think of Function Currying like building a relationship. You don’t propose directly 😅 First comes the “Hi Hello 👋” phase → then friendship ☕ → and finally… the proposal ❤️ In JavaScript, instead of passing all arguments at once, Function Currying lets us pass them step by step, each step returning a new function until the final output is achieved. Here’s a simple code analogy from my video: function proposeTo(crush) { return function (timeSpent) { return function (gift) { return `Dear ${crush}, after ${timeSpent} of friendship, you accepted my ${gift}! 🥰`; }; }; } console.log(proposeTo("Sizuka")("3 months")("red rose 🌹")); Each function takes one argument and returns another function — making the code modular, flexible, and easy to reuse. 👉 This is Function Currying — one argument, one step, one perfect result. 🎥 Watch the full short video here: 🔗 https://lnkd.in/g-NkeYBc --- 💡 Takeaway: Function Currying isn’t just a JavaScript trick — it’s a powerful pattern for cleaner, more composable functions that enhance reusability and maintainability in modern frontend code. --- Would love to know: 👉 What’s your favorite JavaScript concept that clicked instantly when you saw it explained simply? #javascript #frontenddevelopment #webdevelopment #coding #programming #softwareengineering #learnjavascript #100daysofjavascript #techsharingan #developers #careergrowth
To view or add a comment, sign in
-
⚙ Understanding Functions in JavaScript — The Building Blocks of Code A function in JavaScript is like a mini-program inside your main program. It allows you to reuse code, organize logic, and make your code modular. --- 💡 Definition: A function is a block of code designed to perform a particular task. You can define it once and call it multiple times. --- 🧠 Syntax: function greet(name) { console.log("Hello, " + name + "! 👋"); } greet("Kishore"); greet("Santhiya"); ✅ Output: Hello, Kishore! 👋 Hello, Santhiya! 👋 --- 🧩 Types of Functions: 1. Named Function function add(a, b) { return a + b; } 2. Anonymous Function const multiply = function(a, b) { return a * b; }; 3. Arrow Function const divide = (a, b) => a / b; --- ⚙ Why Functions Matter: ✅ Reusability ✅ Readability ✅ Easier debugging ✅ Cleaner, modular code --- 🔖 #JavaScript #WebDevelopment #FunctionsInJS #Frontend #CodingTips #JSConcepts #LearnToCode #100DaysOfCode #KishoreLearnsJS #DeveloperJourney #WebDevCommunity #CodeLearning
To view or add a comment, sign in
-
🚀 New Article: Understanding JavaScript Closures & Lexical Environments Ever wondered what really happens under the hood when JavaScript functions "remember" their variables? I just published a deep dive into closures—one of JavaScript's most powerful (and misunderstood) features. In this article, you'll learn: ✅ How Lexical Environments work behind the scenes ✅ Why functions remember where they were created ✅ The [[Environment]] property every function carries ✅ Memory management and garbage collection with closures ✅ A Chrome DevTools debugging gotcha that might surprise you Whether you're preparing for interviews or leveling up your JS knowledge, understanding closures is essential for writing better code. 🔗 Read the full article on DEV.to: https://lnkd.in/dfY2pJEQ #JavaScript #WebDevelopment #Programming #Closures #SoftwareEngineering #DevCommunity #CodingLife
To view or add a comment, sign in
-
*5 Things I Wish I Knew When Starting JavaScript 👨💻* Looking back, JavaScript was both exciting and confusing when I started. Here are 5 things I wish someone had told me earlier: *1. var is dangerous. Use "let" and "const" "var" is function-scoped and can lead to unexpected bugs. "let" and "const" are block-scoped and safer. *2. JavaScript is asynchronous Things like "setTimeout()"and "fetch()" don’t behave in a straight top-down flow. Understanding the *event loop* helps a lot. *3. Functions are first-class citizens* – You can pass functions as arguments, return them, and even store them in variables. It’s powerful once you get used to it. *4. The DOM is slow – avoid unnecessary access* – Repeated DOM queries and manipulations can hurt performance. Use "documentFragment" or batch changes when possible. *5. Don’t ignore array methods* map(), "filter()" , "reduce()", "find()"… they make code cleaner and more readable. I wish I started using them sooner. 💡 *Bonus Tip:* "console.log()" is your best friend during debugging! If you’re starting out with JS, save this. And if you're ahead in the journey — what do *you* wish you knew earlier? #JavaScript #WebDevelopment #CodingJourney #Frontend #LearnToCode #DeveloperTips
To view or add a comment, sign in
-
Understanding this in JavaScript this — one of the most misunderstood keywords in JavaScript. It’s simple in theory… until it suddenly isn’t 😅 Here’s what makes it tricky — this depends entirely on how a function is called, not where it’s written. Its value changes with context. Let’s look at a few examples 👇 const user = { name: "Sakura", greet() { console.log(`Hello, I am ${this.name}`); }, }; user.greet(); // "Sakura" ✅ const callGreet = user.greet; callGreet(); // undefined ❌ (context lost) const boundGreet = user.greet.bind(user); boundGreet(); // "Sakura" ✅ (context fixed) Key takeaways 🧠 ✅ this refers to the object that calls the function. ✅ Lose the calling object → lose the context. ✅ Use .bind(), .call(), or .apply() to control it. ✅ Arrow functions don’t have their own this — they inherit from the parent scope. These small details often trip up developers in interviews and real-world debugging sessions. Once you understand the context flow, this becomes a lot less scary to work with. 💪 🎥 I simplified this in my Day 28/50 JS short video— check it out here 👇 👉 https://lnkd.in/gyUnzuZA #javascript #webdevelopment #frontend #backend #programming #learnjavascript #softwaredevelopment #developerslife #techsharingan #50daysofjavascript
To view or add a comment, sign in
More from this author
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development