I'm following a tutorial on Javascript and ES6 and in the example below I get an error:
"TypeError: Cannot read property 'teamName' of undefined."
The tutor explains it as: "Whenever we pass an anonymous function somewhere in our code base, the value of this inside of that function is lost!" What does he mean by that? Shouldn't this hold the object making the call and wouldn't that mean team.teamName which would return Avengers here? I'm confused.
Also, how is the issue fixed when we use .bind(this)?
const team = {
members: ['Tony', 'Peter'],
teamName: 'Avengers',
teamSummary: function() {
return this.members.map(function(member) {
return `${member} is a member of ${this.teamName}`;
});
}
};
team.teamSummary();