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 trialAdam Travers
568 PointsCould you elaborate on this please? const invitees = [...]; invitees.forEach(homePage.addInvitee, homePage);
I am unsure why homePage is places after the comma, what is it's purpose and function here specifically?
const invitees = [
'Gonzalo Torres del Fierro',
'Shadd Anderson',
'George Aparece',
'Shadab Khan',
'Joseph Michael Casey',
'Jennifer Nordell',
'Faisal Albinali',
'Taron Foxworth',
'David Riesz',
'Maicej Torbus',
'Martin Luckett',
'Joel Bardsley',
'Reuben Varzea',
'Ken Alger',
'Amrit Pandey',
'Rafal Rudzinski',
'Brian Lynch',
'Lupe Camacho',
'Luke Fiji',
'Sean Christensen',
'Philip Graf',
'Mike Norman',
'Michael Hulet',
'Brent Suggs'
];
invitees.forEach(homePage.addInvitee, homePage);
1 Answer
Rohald van Merode
Treehouse StaffHey Adam Travers 👋
Great question! That second parameter is used to specify the value of this
in the addInvitee
method.
The addInvitee
function in the HomePage
class relies on the values of this.driver
and this.locators
to interact with the page. Those two properties are available within the HomePage
class itself but won't be available when the addInvitee
function is called by forEach
in index.js
as the context will be different.
To make sure that those properties can be accessed within the addInvitee
function you'll have to pass a second argument to the forEach
. This will bind the this
context to the correct homePage
instance. Without it, the this
keyword would not be tied to a specific object and it would default to the global object (window
in browsers). Any references to this.driver
and this.locators
would then end up being undefined or be pointing to the wrong object 🙂
I hope this answers your question 😃 Happy coding!