Skip to the solution
HardFrontend7 min read

What is the Reflect API?

884 views

The problem

Question presented to candidate: "When you write a Proxy trap, you usually call the matching Reflect function inside it to preserve default behavior. Why does Reflect exist as a separate API at all β€” what does it actually provide that the older, direct operators and methods (like the 'in' operator, or Function.prototype.apply) don't?"

What a strong answer should cover:

  • πŸ“Œ Interview term: Reflect β€” a built-in object providing function versions of JavaScript's own internal operations (Reflect.get, .set, .has, .ownKeys, .apply, .construct, and more) β€” each one mirrors an operation that previously only existed as an operator or a scattered method, now callable directly as a real function.
  • πŸ“Œ Verified, not assumed: Reflect.set() on a real frozen object genuinely returned a real false β€” a clean, direct boolean signal β€” while the equivalent direct assignment in sloppy mode genuinely gave no signal at all either way, confirmed directly.
  • πŸ“Œ Interview term: Reflect.ownKeys() β€” genuinely returns both string and Symbol keys together in one real array, verified directly to differ from Object.keys(), which genuinely excludes Symbols (covered in this bank's own dedicated question).
  • A precise answer names the real, canonical pairing with Proxy: every trap has a matching Reflect function providing the exact real default behavior β€” verified directly, a real get trap correctly delegating to Reflect.get(target, prop, receiver) (correctly receiver-aware, unlike a plain target[prop]) is the standard, correct way to preserve normal behavior while adding interception.
  • A precise answer names Reflect.construct() as a real, direct way to invoke a constructor with an arguments array, without needing new Fn(...args) syntax β€” verified directly, it genuinely produced a real, correct instanceof-passing instance.

Clarifying questions expected:

  • "Is Reflect being used standalone for its cleaner boolean-return API, or specifically paired inside a Proxy trap to preserve default behavior?" β€” both are real, genuine uses, verified above, with Proxy-pairing being the more common real-world case.
  • "Does the actual code need receiver-aware behavior (relevant for a get/set trap on a prototype chain), which Reflect's functions correctly support and a plain target[prop] access does not?"

Code / implementation expected: Yes β€” a real, direct comparison of Reflect.set's clean boolean return versus a sloppy-mode direct assignment's genuine silence, plus a real Reflect.ownKeys call returning both string and Symbol keys together, is the concrete, convincing proof of exactly what Reflect adds.

  • reflect

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: Reflect's clean boolean returns, combined string+Symbol key enumeration, and constructor calls without new syntax

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 131 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

What are Symbols and what are they used for?

Continue