var Song function function Song artist title this artist artist this t

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var Song = (function () {
function Song(artist, title) {
this.artist = artist;
this.title = title;
}
Song.prototype.play = function () {
console.log('Playing ' + this.artist + ' - ' + this.title);
};
Song.prototype.stop = function () {
console.log('Stopping ' + this.artist + ' - ' + this.title);
};
return Song;
})();
var MusicPlayer = (function () {
function MusicPlayer() {
this.currentSongIndex = -1;
this.songsList = [];
}
MusicPlayer.prototype.add = function (song) {
this.songsList.push(song);
};
MusicPlayer.prototype.next = function () {
if (this.songsList[this.currentSongIndex + 1]) {
if (this.currentSongIndex != -1) {
this.songsList[this.currentSongIndex].stop();
}
this.songsList[++this.currentSongIndex].play();
}
};
MusicPlayer.prototype.prev = function () {
if (this.songsList[this.currentSongIndex - 1]) {
this.songsList[this.currentSongIndex].stop();
this.songsList[--this.currentSongIndex].play();
}
};
return MusicPlayer;
})();
// теперь с сервера загружаем
var songs = [
{ artist: "Linkin Park", title: "What I've Done" },
{ artist: "Depeche Mode", title: "Enjoy The Silence" },
{ artist: "System Of A Down", title: "Toxicity" }
];
var player = new MusicPlayer();
for (var i in songs) {
var song = new Song(songs[i].artist, songs[i].title);
player.add(song);
}
player.next();
player.next();
player.prev();
player.next();
player.next();