DEV Community

bomoniyi
bomoniyi

Posted on

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).

Top comments (0)