我對JavaScript的OOP很陌生,想弄清楚如何創建一個類並傳遞對象的值(我知道JS沒有類,所以我正在玩與原型周圍)。在這個練習的例子中,我試圖創建一個有多個書架的書架,每個書架都有幾本書。我正在尋找將書架上的書架,書架,架子(以及書架上的書架)數量傳遞給圖書館。任何幫助將不勝感激。謝謝!用Javascript創建一個超類(繼承)
這裏是我的代碼看起來像至今:
//LIBRARY
function Library (name)
{
this.name = name;
}
var lib = new Library("Public");
//SHELVES
Shelves.prototype = new Library();
Shelves.prototype.constructor=Shelves;
function Shelves (name, shelfnum)
{
this.name = name;
this.shelfnum = shelfnum;
}
var famous = new Shelves("Famous", 1);
var fiction = new Shelves("Fiction", 2);
var hist = new Shelves("History", 3);
// BOOKS
Book.prototype = new Shelves();
Book.prototype.constructor=Book;
function Book (name, shelf)
{
this.name = name;
this.shelf = shelf;
}
var gatsby = new Book("The Great Gatsby", 1);
var sid = new Book("Siddhartha",1);
var lotr = new Book("The Lord of The Rings", 2);
var adams = new Book("John Adams", 3);
OT:這沒有意義。爲什麼'Shelves'從'Library'和'Book'從'Shelves'延伸?最好讓圖書館擁有一個書架清單,每個書架都有一個書目清單。 –
爲了擴大@IngoBürk所說的內容,一個書架不是一個圖書館,一本書不是一個書架。 –