原型继承2

function Dice(sides){
	this.sides=sides;
	this.roll=function(){
		var randomNumber=Math.floor(Math.random()*this.sides)+1;
		return randomNumber;
	}
}
var dice=new Dice(6);

  

function Dice(sides){
	this.sides=sides;
}
Dice.prototype.roll=function(){
	var randomNumber=Math.floor(Math.random()*this.sides)+1;
	return randomNumber;
}
var dice=new Dice(6);

  

 

 

function Song(title,artist,duration) {
	this.title=title;
	this.artist=artist;
	this.duration=duration;
	this.isPlaying=false;
}

Song.prototype.play = function() {
	this.isPlaying=true;
};

Song.prototype.stop = function() {
	this.isPlaying=false;
};

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 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");


playlist.add(hereCo);
playlist.add(hddeCo);

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);

}

  

posted @ 2015-07-08 21:14  bjsunhe  阅读(165)  评论(0编辑  收藏  举报