Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Prototype Method

Object-Oriented Programmingmedium

Course · Section 14: Object-Oriented Programming (OOP) With JavaScript · Lecture 222: Prototypes

Using a constructor function `Box(side)` with a `volume()` method on its prototype, implement `boxVolume(side)` returning the cube of the side.

Sample tests

Input: boxVolume(2)
Output: 8
Input: boxVolume(1)
Output: 1

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Defining the method inside the constructor creates a new copy per instance.
Visualize this concept: Prototypes and Inheritance →
Approach & explanation (try first)

A prototype method is shared across instances and reads instance data through this.

Loading...
⌘/Ctrl + Enter

Run your code to see results.