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 trialMarcus Schumacher
16,616 PointsCan someone explain line 8?
How the getKeys function of Person is return Object.keys(this);
How does the keys method of Object know what to return when provided with 'this' as a parameter?
1 Answer
Steven Parker
231,269 PointsWhen the "getkeys" method is invoked on an object (of type Person), the value of "this" is a reference to the current object instance. So when you pass it to "Object.keys", it returns the keys in the current object.
But as demonstrated in the video, if the function is invoked from a different context, "this" might not refer to an object at all and an error could occur. But the video goes on to fix this by using an arrow function. After that "this" refers to the object again.
You might enjoy the short workshop Understanding "this" in JavaScript.