JavaScript call, apply and bind — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript call, apply and bind 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 call
easy10 minUse call to run a greeting with the person object as this.
- 2
Pass arguments with call
easy10 minUse call to pass a city as well as setting this.
- 3
Use apply
easy10 minDo the same job with apply, passing the arguments as an array.
- 4
Use bind
easy10 minUse bind to make a greeting function that always uses the person object.
- 5
Borrow a method
medium10 minUse call so the same function reports two different names, joined by a comma.
Find the bug(5)
- 1
Fix the missing this value
easy8 minCalling the function directly loses the object.
- 2
Fix apply given loose arguments
easy8 minapply needs its arguments inside an array.
- 3
Fix bind used like call
easy8 minbind returns a function; it does not run it.
- 4
Fix the argument in the wrong place
easy8 minWith call, this comes first and the arguments follow.
- 5
Fix the same object used twice
easy8 minBoth calls borrow the method with the same object.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace