Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Object Method with this

Fundamentals: Part 2medium

Course · Section 3: JavaScript Fundamentals – Part 2 · Lecture 45: Object Methods

Implement `rectangleArea(w, h)` by building an object with width, height, and an `area()` method that uses `this`, then return its area.

Sample tests

Input: rectangleArea(3, 4)
Output: 12
Input: rectangleArea(5, 5)
Output: 25

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Detaching the method loses its this binding.
Approach & explanation (try first)

An object method uses this to reach the object's own width and height.

Loading...
⌘/Ctrl + Enter

Run your code to see results.