Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Prototype Method

Object-Oriented Programmingmedium

Course · Section 14: Object-Oriented Programming (OOP) With JavaScript · Lecture 226: ES6 Classes

Using a constructor function and its prototype (not the `class` keyword), give `Speaker` a `greet()` method. Implement `greetWith(name)` returning `Hi ` followed by the name.

Sample tests

Input: greetWith("Sam")
Output: "Hi Sam"
Input: greetWith("A")
Output: "Hi A"

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Defining the method inside the constructor creates a new function per instance.
  • Forgetting new loses the this binding.

Learning resources

  • MDN: Classes
Visualize this concept: Prototypes and Inheritance →
Approach & explanation (try first)

Putting greet on the prototype shares one method across instances, the mechanism classes use under the hood.

Loading...
⌘/Ctrl + Enter

Run your code to see results.