How to use a function inside a function inside a class?

Good day!

There is a service in angular (for music listening).
here it is:
export class PlayService {
 private audios = [];
 private nowPlaying = 0;
 private player = new Audio();

 public addAudios(data, nowPlaying){
 this.audios = [];
 this.audios = data;
 this.nowPlaying = nowPlaying;
 console.log('sending End');

 console.log('Start of audio file');
 this.player.src = 'src/files/'+this.audios[nowPlaying]['file'];
this.player.play();

 this.player.addEventListener('ended', function (){
 this.nextSong(); // swears here
 console.log('Audio file ended');
});
}
 public nextSong(){
 console.log('Start next audio file');
this.nowPlaying++;
 this.player.src = 'src/files/'+this.audios[this.nowPlaying]['file'];
this.player.play();
}
}


The music itself works, but not working the switch at the end
Swears by this.nextSong();. That I do not understand what's wrong here.. I tried without this and tried self
it does not work.. what it could be?
August 19th 19 at 22:47
2 answers
August 19th 19 at 22:49
Solution
Arrow functions like it just to make this push.
That's not working?
this.player.addEventListener('ended', () => {
this.nextSong();
 console.log('Audio file ended');
 });
Pancake, precisely, thanks huge!!! Already 4 hours in agony.. - Akeem commented on August 19th 19 at 22:52
August 19th 19 at 22:51
Solution
And similarly, with the self properly tried?
var self = this;
this.player.addEventListener('ended', function (){
self.nextSong();
 console.log('Audio file ended');
 });

Should work. But honestly, so now men do not write. Not ES6'ovsk! It is necessary to write so:
this.player.addEventListener('ended', () => {
this.nextSong();
 console.log('Audio file ended');
 });
Thank You very much!) - Akeem commented on August 19th 19 at 22:54

Find more questions by tags AngularJavaScript