爲什麼this.name = name;
在企鵝和皇帝班,爲什麼this.numLegs = numLegs;
被遺漏了?是因爲你可以設置這些屬性嗎?javascript inheritence爲什麼財產被遺漏在一些?
// original classes
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
this.isAlive = true;
}
function Penguin(name) {
this.name = name;
this.numLegs = 2;
}
function Emperor(name) {
this.name = name;
this.saying = "Waddle waddle";
}
// set up the prototype chain
Penguin.prototype = new Animal();
Emperor.prototype = new Penguin();
var myEmperor = new Emperor("Jules");
//console.log(myEmperor.saying); // should print "Waddle waddle"
//console.log(myEmperor.numLegs); // should print 2
//console.log(myEmperor.isAlive); // should print true
console.log(myEmperor.name);
答案是因爲開發人員選擇不從構造函數中的參數設置該屬性。這只是一個設計決定。 – marteljn
請詳細說明一下。 – Mozak
對不起,但我找不到這個腳本的一些問題。 – Red