Currently, I am attempting to include a property in an object by utilizing a method. Below is the code snippet:
const siddhu = {
name: 'Siddhu',
friends: ['Dylan', 'Jordans', 'Aathi'],
setBestFriend: () => this.bestFriend = this.friends[0],
setNumOfFriends: () => this.numOfFriends = this.friends.length,
}
siddhu.setBestFriend()
siddhu.setNumOfFriends()
console.log(`${siddhu.name} has ${siddhu.numOfFriends} friends, and his best friend is ${siddhu.bestFriend}`);
I have encountered an issue where this code does not function as expected. Although replacing the this
keyword with siddhu
resolves the problem, it is not an ideal solution because I intend to use this code multiple times and changing siddhu
each time is inconvenient.