在Javascript中创建原型对象的目的是什么?

时间:2016-08-10 00:20:29

标签: javascript prototype

我正在学习JS,但对Song.prototype = Object.create(Media.prototype);Movie.prototype = Object.create(Media.prototype);部分的以下内容感到困惑。如果Media函数在范围内,为什么需要它们?

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


Media.prototype.start = function start() {
  this.isPlaying = true;
};

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

function Song(title, artist, duration) {
  Media.call(this, title, duration);
  this.artist = artist;
}

Song.prototype = Object.create(Media.prototype);

function Movie(title, year, duration) {
  Media.call(this, title, duration);
  this.year = year;
}

Movie.prototype = Object.create(Media.prototype);

0 个答案:

没有答案