Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Borrow a Method with call

A Closer Look at Functionsmedium

Course · Section 10: A Closer Look at Functions · Lecture 140: The call and apply Methods

Given a standalone `greet` function that uses `this.name`, implement `greetWith(person, greeting)` that invokes it on `person` using `call`, returning `greeting`, a comma, and the name.

Sample tests

Input: greetWith({"name":"Ada"}, "Hi")
Output: "Hi, Ada"
Input: greetWith({"name":"Bo"}, "Hey")
Output: "Hey, Bo"

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Calling greet directly leaves this undefined (or the global object).
Approach & explanation (try first)

call invokes the function with this bound to person and the greeting passed as the first argument.

Loading...
⌘/Ctrl + Enter

Run your code to see results.