0

Why does this refers to global object if I console.log inside this.movies.forEach(function(movie) { i.e in this snippet

var Actor = {
  name: 'RajiniKanth',
  movies: ['Kabali', 'Sivaji', 'Baba'],
  showMovies: function() {
   this.movies.forEach(function(movie) {
     // this.name is going to be empty here
     console.log(this) // Why is this global object here?
     console.log(this.name + " has acted in " + movie);
   });
  }
};
Cyberduck
  • 31
  • 6

0 Answers0