Understanding <code>this</code> with Arrow Functions
📖 Scenario: Imagine you are creating a simple object to represent a person. You want to understand how the this keyword behaves differently in regular functions and arrow functions inside that object.
🎯 Goal: You will build an object called person with a name property and two methods: one using a regular function and one using an arrow function. You will observe how this works in each method.
📋 What You'll Learn
Create an object called
person with a property name set to 'Alice'.Add a method called
sayHelloRegular using a regular function that returns a greeting using this.name.Add a method called
sayHelloArrow using an arrow function that returns a greeting using this.name.Call both methods and print their results to see the difference in
this behavior.💡 Why This Matters
🌍 Real World
Understanding <code>this</code> is crucial when writing JavaScript code for web pages, apps, or servers, especially when working with objects and event handlers.
💼 Career
Many JavaScript jobs require clear knowledge of how <code>this</code> works to avoid bugs and write clean, maintainable code.
Progress0 / 4 steps