I'm very new to OOP in JavaScript and am trying to figure out how to create a class and pass values from objects (I know JS doesn't have classes so I was playing around with the prototype). In this practice example I am trying to create a class "Library" that has multiple shelves and each shelf has a couple books. I am looking to pass what shelf the book is on (shelf) from books to shelves and the number of shelves (and the books that are on them) to library. Any help would be greatly appreciated. Thanks!
Here is what my code looks like so far:
//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);
Shelvesextend fromLibraryandBookfromShelves? It would be better to have the library hold a list of shelves, each of which maintains a list of books.