Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

this in a Method

Behind the Scenesmedium

Course · Section 8: How JavaScript Works Behind the Scenes · Lecture 100: The this Keyword

Implement `personGreeting(name)` by building an object with a `greet()` method that uses `this.name`, returning `Hi ` plus the name.

Sample tests

Input: personGreeting("Ada")
Output: "Hi Ada"
Input: personGreeting("Bo")
Output: "Hi Bo"

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • An arrow method would not get its own this binding here.
Visualize this concept: The this Keyword →
Approach & explanation (try first)

Calling greet on the object sets this to that object, so this.name is the person's name.

Loading...
⌘/Ctrl + Enter

Run your code to see results.