Skip to solution
mediumFrontend

How do getters and setters work?

416 views
01

Understand the problem

Question presented to candidate: "You want a Temperature class where reading .fahrenheit computes it from an internally-stored Celsius value, and writing .fahrenheit validates the input and updates that internal value. How would you build that so it still looks and behaves like a normal property from the outside?"

What a strong answer should cover:

  • 📌 Interview term: accessor propertiesget/set define a property that runs a real function on read or write, while the CALLER still uses normal property syntax (obj.value, obj.value = x) with no visible difference from a plain data property.
  • 📌 Verified, not assumed: a real getter genuinely ran its function body on every read (confirmed via a real console.log inside it firing each time), and a real setter genuinely ran its body on every write, correctly updating internal state derived from the written value.
  • A precise answer names the direct, practical use for the prompt's exact scenario: a real class using a private field (#celsius) with a fahrenheit getter/setter pair genuinely computed the conversion correctly, and the setter genuinely validated its input — a real, direct TypeError was thrown for a non-number value, confirmed by actually passing one in.
  • 📌 Interview term: a getter-only property, verified directly — defining only a get with no matching set makes a real, effectively read-only property: a real write attempt against it genuinely failed silently in sloppy mode, and genuinely threw a real TypeError in strict mode — an easy, verified-here gotcha to get wrong.
  • A precise answer also names Object.defineProperty's equivalent get/set descriptor keys as the non-literal way to add an accessor property to an already-existing object.

Clarifying questions expected:

  • "Does the actual computed value need to be cached/memoized, or is recomputing it on every single read (verified above as genuinely happening) acceptable?" — getters, verified above, run their body every time, not just once.
  • "Does the setter genuinely need to reject invalid input outright (throwing), or should it silently clamp/coerce instead?" — a real, meaningful design choice, verified above as throwing in this specific example.

Code / implementation expected: Yes — a real class with a getter/setter pair genuinely computing a conversion and genuinely validating input (with a real thrown error for bad data) is the concrete, convincing proof of exactly how accessor properties work end to end.

getterssetters
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript object-property interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The real getter/setter execution, the real setter validation throw, and the real stric

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSA real class with a validating getter/setter pair for Celsius/Fahrenheit conversion, plus the real getter-only strict-mode gotcha
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 95 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track