我对 JavaScript 中的 OOP 非常陌生,并且正在尝试弄清楚如何创建一个类并从对象传递值(我知道 JS 没有类,所以我在玩原型)。在这个实践示例中,我试图创建一个具有多个书架的“图书馆”类,每个书架都有几本书。我正在寻找将书在书架上的书架(书架)从书架传递到书架以及书架的数量(以及书架上的书)到图书馆。任何帮助将不胜感激。谢谢!
这是我的代码到目前为止的样子:
//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);