码迷,mamicode.com
首页 > 其他好文 > 详细

原型继承3

时间:2015-07-08 22:31:17      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

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

}

  

原型继承3

标签:

原文地址:http://www.cnblogs.com/sunhe/p/4631405.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!