[JS]模擬類別的繼承

寫JS一陣子之後才知道要怎麼模擬繼承的寫法...

多虧平常一起共事的YY女同事寫了很多不錯的程式碼可以參考。

//父類別-人
function People() {
  this.hands = 2; //2隻手
  People.prototype.say = function(){
    return "I am People";
  }
}
	
//子類別-傷心的人
function SadPeople() {
  People.call(this);
  this.slowMelody = false; //聽慢歌
}

var prototype = Object.create(People.prototype);
prototype.constructor = SadPeople;
SadPeople.prototype = prototype;

//測試
var sadPeople = new SadPeople();
console.log(sadPeople.hands); 	   // 2
console.log(sadPeople.slowMelody); // false;
console.log(sadPeople.say());      // I am People