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 trialSadaf Partovi
Front End Web Development Techdegree Graduate 14,594 PointsaddBook()?
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) } }
Sadaf Partovi
Front End Web Development Techdegree Graduate 14,594 Pointsrelated to https://teamtreehouse.com/library/practice-object-interaction practice.
2 Answers
Peter Vann
36,427 PointsConsidering 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.
Sadaf Partovi
Front End Web Development Techdegree Graduate 14,594 Pointsthanks for taking the time and explaining it :) make more sense now.
Peter Vann
36,427 PointsTo 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
Paul Messmer
15,023 PointsPaul Messmer
15,023 PointsWhat video is this regarding?