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<noob />
17,062 Pointswhy in order to point to the books array i need to add the “this” keyword
I understand that “this” refers to the object the we are referring too which in this case is the library class which contain 2 arrays, and because we want to add books to the books array we use this.books?
still doesn’t make sense to me.
2 Answers
Milan Toth
12,182 PointsExactly. The keyword this refers to the current instance of the class. If each of your instance have an array with different items in it, this will point to those. Therefore to correctly access them in each instance, you will need to use this.
<noob />
17,062 Pointscan u explain in more in depth? what do u mean by : “If each of your instance have an array with different items in it”
i don’t create an instance of the library class to access the books array in the addBook method i simply use this and i don’t fully understand why i can’t just say: books.push
Milan Toth
12,182 PointsIt's explained in the next video when you instantiate the Library class with const library = new Library(); then the library variable will be an instance if the Library class and this will point to the current instance, which is the library constant. But you can have multiple instances of the Library class and that's when this comes into play.
example: const library = new Library(); const library2 = new Library();
in these instances this will point to the current instance, so this.books means library.books in the library instance and this.books will be library2.books in the second instance.
I hope it makes a little bit more sense, I know it can be quite overwhelming first, but it will make sense eventually :)
<noob />
17,062 PointsI understand what ur example, if i want to add for example another book to a new instance, for example:
const library3 = new Library();
addBook(book) {
this.books.push(book);
}
here i created anothr instance of the Library class, now i have 3 instances of the class (library1,library and library3) how can i point to each of them in this case? it's all depends on which instance of the class i activate the method on?
const exampleBook = new Book("test title","test author","test isbn");
library3.addBook(exampleBook);
and when i execute this call "this" now points to the library3 instance because this is the current instance im adding a book to?
Steven Parker
231,269 PointsSteven Parker
231,269 PointsI got notified by the tag but it looks like Milan's already got you covered. That makes a good point, lots of folks are willing to help, try giving a fresh question some time (24 hours) before tagging anyone specifically.