Can You Predict the Output? If you’re working with JavaScript, understanding how this behaves is the difference between clean code and hours of debugging. 🧐 What’s the result? A) Hello B) undefined C) ReferenceError D) null Drop your answer in the comments before reading the explanation below! The Explanation : This is a classic "Context" trap. Here is why the answer is B) undefined: - The Outer Function: When obj.innerMessage() is called, this correctly points to obj. - The IIFE: Inside innerMessage, we have an Immediately Invoked Function Expression (IIFE). In non-strict mode, when a regular function is invoked like this (not as a method of an object), its this context defaults to the global object (window in browsers). - The Result: Since the global object doesn't have a property called message, it returns undefined. - The Bonus undefined: The final console.log(obj.innerMessage()) prints an extra undefined because innerMessage doesn't have a return statement! How to fix it? 🛠️ To get "Hello", you would use an Arrow Function for the IIFE, as arrow functions lexically bind this from the surrounding scope. #JavaScript #MERNStack #WebDevelopment #ReactJS #NodeJS #CodingChallenge #SoftwareEngineering #Hiring #FrontendDeveloper
Undefined
IIFE inside a function which is a property of an object? Why would anyone use this?