Use call to run a greeting with the person object as this.
Acceptance criteria
- 1.#out should show Hello Ada.
JavaScript Tutorial · JavaScript Functions
These 10 tasks go with the JavaScript call, apply and bind chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “JavaScript call, apply and bind let you choose what the this keyword refers to inside a function.” .
1.Use callexercise · easy · 10 min
Use call to run a greeting with the person object as this.
Passes when: #out should show Hello Ada.
2.Pass arguments with callexercise · easy · 10 min · practice plan
Use call to pass a city as well as setting this.
Passes when: #out should show Ada in London.
3.Use applyexercise · easy · 10 min · practice plan
Do the same job with apply, passing the arguments as an array.
Passes when: #out should show Ada in Paris.
4.Use bindexercise · easy · 10 min · practice plan
Use bind to make a greeting function that always uses the person object.
Passes when: #out should show Hello Grace.
5.Borrow a methodexercise · medium · 10 min · practice plan
Use call so the same function reports two different names, joined by a comma.
Passes when: #out should show Ada,Grace.
6.Fix the missing this valuebug fix · easy · 8 min
Calling the function directly loses the object.
Passes when: #out should show Hello Ada.
7.Fix apply given loose argumentsbug fix · easy · 8 min · practice plan
apply needs its arguments inside an array.
Passes when: #out should show Ada in Paris.
8.Fix bind used like callbug fix · easy · 8 min · practice plan
bind returns a function; it does not run it.
Passes when: #out should show Hello Grace.
9.Fix the argument in the wrong placebug fix · easy · 8 min · practice plan
With call, this comes first and the arguments follow.
Passes when: #out should show Ada in London.
10.Fix the same object used twicebug fix · easy · 8 min · practice plan
Both calls borrow the method with the same object.
Passes when: #out should show Ada,Grace.
Between them these tasks cover call, apply and bind.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.