简体   繁体   中英

Create a super class with Javascript (inheritance)

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);

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: ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM