JavaScript this Keyword — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript this Keyword lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Use this in a method
easy10 minReturn a greeting using this.name from an object method.
- 2
this is the object before the dot
easy10 minReturn a description using this.brand.
- 3
Keep this with bind
easy10 minBind a method to its object and call it from a separate variable.
- 4
An arrow keeps the outer this
easy10 minUse an arrow inside a method so it can still see the object.
- 5
this in a class
medium10 minCreate a class with a speak method and show what one instance says.
Find the bug(5)
- 1
Fix the missing this
easy8 minThe method reads a name that does not exist in its scope.
- 2
Fix the method that lost its object
easy8 minTaking the method out of the object breaks the link to this.
- 3
Fix bind that was never called
easy8 minbind returns a function; it does not run it.
- 4
Fix the normal function inside map
easy8 minA normal callback has its own this, so the team name is lost.
- 5
Fix the class field never set
easy8 minThe constructor does not store the name on the instance.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace