简体   繁体   中英

Javascript data structure: Object oriented or array?

Let's say I have two classes in a object orientated paradigm : libraries and books. They have respectively the libraryName and bookName properties. Each library contain an array of books (of unfixed size).

From one book, I would like to obtain the name of its libray, and from a library the name of all the books it contains.

What is the best way implement this in Javascript (preferably with object oriented Javascript) ?

For example, my code could start like this:

function library(libraryName){
     this.LibraryName=libraryName;
     this.books = new Array();
}

You could build a method in the Library class to achieve this. For example:

Define the Library object.

function Library (name) {
    this.libraryName = name;
    this.books = [];
}

Define a prototype method 'addBook'

Library.prototype.addBook = function(book) {
    book.libraryName = this.libraryName;
    this.books.push(book);
}

As you can see in the addBook method, the book is given the name of the library and added to the library's book array. Alternatively you could add a reference to the library object itself.

Alternative prototype method 'addBook'

Library.prototype.addBook = function(book) {
    book.library = this;
    this.books.push(book);
}

This ensures if the library name is updated later on, fetching it from the Book object will point directly to the library object and therefore stay up to date.

Next we define the Book object.

function Book (name) {
    this.bookName = name;
    this.library = {};
}

That's all we need. Now we can interface with these objects.

//First we declare the objects
var hp = new Book("Harry Potter"),
    lib = new Library("Village Library");

//Add the book to the library
lib.addBook(hp);

//Outputs 'Village Library'
console.log(hp.library.libraryName);

lib.libraryName = "The new Library";

//Outputs 'The new Library'
console.log(hp.library.libraryName);    

As an extra, if you wanted the ability for a book to be in multiple libraries, just change the Book object slightly like so:

function Book (name) {
    this.bookName = name;
    this.libraries = [];
}

You'll also have to modify the 'addBook' method.

Library.prototype.addBook = function(book) {
    book.libraries.push(this);
    this.books.push(book);
}

And that's it!

Assume, that no matter what is the language, you need or have pointer in the book to the library or loop all libraries and books with comparing book name

var Library = function (name, books) {
    this.name = name;
    this.books = books;
}


var Book = function (name) {
    this.name = name;
}

var libraries = [new Library("lib1", [ new Book("book1"), new Book("book2") ])];

function findBooksLibrary(bookName) {
    var libName;

    libraries.some(function (library) {
        var res = library.books.some(function (book) {
            book.name === bookName;
        });
        if (res) {
            libName = library.name;
            return true;
        } else return false;
    });

    return libName;

}

findBooksLibrary("book1");

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