Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

addBook()?

the addBook() method has one parameter which is "book". Is this parameter the same as the Book class? In the library class, we have "books" and in the Book class we have Book with the capital "B", so where does this "book" in the addBook() method come from?? Thanks in advance:)

class Library { constructor() { this.books = []; this.patrons = [];

}

addBook(book) { this.books.push(book); }

addPatron(patron) { this.patrons.push(patron) } }

What video is this regarding?

2 Answers

Considering this code:

class Library {
    constructor(){
        this.books = [];
        this.patrons = [];
    }

    addBook(book) {
        this.books.push(book);

    addPatron(patron) {
        this.patrons.push(patron);
    }

const library = New Library();
const myBook = new Book('Harry Potter and the Sorcerer\'s Stone', 'J.K Rowling', '978-0439708180');
library.addBook(myBook);

'book' is the parameter passed into the 'library' instance's addBook function. Within that function, 'book' could be any word, but 'book' makes the most sense, in this context. 'myBook' is an instance of the Book class and is what is actually passed into the function at this line:

library.addBook(myBook);

Inside the function, 'myBook' is referenced as just 'book', because of how the function is declared.

Notice, also, that I changed

const book =

to

const myBook = 

in order to make the example more clear.

thanks for taking the time and explaining it :) make more sense now.

To really prove it, save this code to a file:

class Book {
    constructor(title, author, isbn){
        this.title = title;
        this.author = author;
        this.isbn = isbn;
    }
}

class Library {
    constructor(){
        this.books = [];
        this.patrons = [];
    }

    addBook(book) {
        this.books.push(book);
    }

    addPatron(patron) {
        this.patrons.push(patron);
    }
}

const library = new Library();
const myBook = new Book('Harry Potter and the Sorcerer\'s Stone', 'J.K Rowling', '978-0439708180');
library.addBook(myBook);

console.log(library.books[0].title);

such as 'test.js' and then run it with

node test.js

from the terminal/command line (if you know how to use node.js) and it should log:

Harry Potter and the Sorcerer's Stone

or you can see it here: https://codepen.io/petrovnikov/pen/xxEQVJB