function Media(title,duration) {
this.title=title;
this.duration=duration;
this.isPlaying=false;
}
Media.prototype.play = function() {
this.isPlaying=true;
};
Media.prototype.stop = function() {
this.isPlaying=false;
};
function Song(title,artist,duration) {
Media.call(this,title,duration);//继承
this.artist=artist;
}
Song.prototype=Object.create(Media.prototype);//继承
Song.prototype.toHTML = function() {
var htmlString=‘<li‘;
if(this.isPlaying){
htmlString+=‘ class="current"‘;
}
htmlString+=‘>‘;
htmlString+=this.title;
htmlString+=‘ - ‘;
htmlString+=this.artist;
htmlString+=‘<span class="duration">‘;
htmlString+=this.duration;
htmlString+=‘</span></li>‘;
return htmlString;
};
function Movie(title,year,duration) {
Media.call(this,title,duration);
this.year=year;
}
Movie.prototype=Object.create(Media.prototype);
Movie.prototype.toHTML = function() {
var htmlString=‘<li‘;
if(this.isPlaying){
htmlString+=‘ class="current"‘;
}
htmlString+=‘>‘;
htmlString+=this.title;
htmlString+=‘ (‘;
htmlString+=this.year;
htmlString+=‘) ‘;
htmlString+=‘<span class="duration">‘;
htmlString+=this.duration;
htmlString+=‘</span></li>‘;
return htmlString;
};
function Playlist() {
this.songs=[];
this.nowPlayingIndex=0;
}
Playlist.prototype.add = function(song) {
this.songs.push(song);
};
Playlist.prototype.play = function() {
var currentSong=this.songs[this.nowPlayingIndex];
currentSong.play();
};
Playlist.prototype.stop = function(){
var currentSong=this.songs[this.nowPlayingIndex];
currentSong.stop();
};
Playlist.prototype.next = function() {
this.stop();
this.nowPlayingIndex++;
if(this.nowPlayingIndex===this.songs.length){
this.nowPlayingIndex=0;
}
this.play();
};
Playlist.prototype.renderInElement = function(list) {
list.innerHTML="";
for(var i=0;i<this.songs.length;i++){
list.innerHTML+=this.songs[i].toHTML();
}
};
var playlist=new Playlist();
var hereCo=new Song("her","the b","2:00");
var hddeCo=new Song("herdd","the bdd","2:0d");
var manof=new Movie("manof","2013","12:90:99");
playlist.add(hereCo);
playlist.add(hddeCo);
playlist.add(manof);
var playlistElement=document.getElementById("playlist");
playlist.renderInElement(playlistElement);
var playButton=document.getElementById("play");
playButton.onclick=function(){
playlist.play();
playlist.renderInElement(playlistElement);
}
var nextButton=document.getElementById("next");
nextButton.onclick=function(){
playlist.next();
playlist.renderInElement(playlistElement);
}
var stopButton=document.getElementById("stop");
stopButton.onclick=function(){
playlist.stop();
playlist.renderInElement(playlistElement);
}
原文:http://www.cnblogs.com/sunhe/p/4631405.html