Using arrow functions to manage the this. function

bomoniyi

bomoniyi

Posted on February 26, 2023

Using arrow functions to manage the this. function
const userData = { 
  username: "Reed",
  title: "JavaScript Programmer",
  getBio() {
    console.log(`User ${this.username} is a ${this.title}`);
  },
  askToFriend() {
    setTimeout(() => {
      console.log(`Would you like to friend ${this.username}?`);   
    }, 2000);  
  } 
}

userData.askToFriend();
Enter fullscreen mode Exit fullscreen mode

If you have a function in a function, use the arrow function to make sure the this. function still pulls from the above function (example in code above).

💖 💪 🙅 🚩
bomoniyi
bomoniyi

Posted on February 26, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024