0

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();
}
3
  • If you never need to resolve the other way around (given library, list all books in it), you could just use one object for the books with a property for the library and be done with it. Commented Nov 28, 2014 at 10:57
  • Post some code - what do you mean by "object oriented" and array, exactly? Would be easier to reason then. Commented Nov 28, 2014 at 10:57
  • @Sirko: The matter is I need to do it in the two ways Commented Nov 28, 2014 at 11:00

2 Answers 2

2

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!

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

Comments

1

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

1 Comment

Could you give me an example in Javascript ?

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.