1

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);
2
  • 4
    OT: This doesn't make sense. Why would Shelves extend from Library and Book from Shelves? It would be better to have the library hold a list of shelves, each of which maintains a list of books. Commented Oct 26, 2013 at 22:20
  • To expand on what @IngoBürk said, a shelf is not a library, and a book is not a shelf. Commented Oct 26, 2013 at 23:08

1 Answer 1

2

As Ingo said in the comment, your example would not be a good candidate for inheritance. Inheritance is when an object is shares features with another type.
Inheritance Example: A Bannana function would inherit from a Fruit function. A Truck function would inherit from an automobile function.

In both cases, the more specific object inherits from a broader category. When you can use multiple inheritance, you may wish to add features to objects by inheriting utility functions: i.e. Maybe all of your functions can inherit from a function that logs errors in a certain way. Then the functions all have access to the error logging methods.

In your case however, you should pursue a different strategy to structure your program using arrays or lists because Libraries have many shelves, but shelves do not exhibit the same characteristics of a library so are not candidates for inheritance.

Here is how I would do it:

function Library(name) {
     this.name = name;
     this.shelves = new Array();
}
function Shelf(name, num){
     this.name = name;
     this.num = num;
     this.books = new Array();
}
function Book(name) {
     this.name = name;
 }

var lib = new Library("Lib");
lib.shelves.push(new Shelf("Classics",1));
lib.shelves.push(new Shelf("Horror", 2));

//shelves[0] is Classics
lib.shelves[0].books.push(new Book("The Great Gatsby"));  
lib.shelves[0].books.push(new Book("The Lord of the Rings")); 

//shelves[1] is Horror
lib.shelves[1].books.push(new Book("Dr. Jekyll and Mr. Hyde")); 



console.log(lib.shelves.length); //# of Shelves in library
console.log(lib.shelves[0].books.length); //# of books in Classics shelf

Hopefully that helps with your project. When you have projects that require OOP in Javascript, this can be helpful: Mozilla: Javascript OOP

Sign up to request clarification or add additional context in comments.

1 Comment

This makes a lot more sense - thanks for including the Mozilla source, that was really helpful as well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.