Skip to solution
mediumDSA

How would you use Reflect.ownKeys() inside a Proxy to correctly enumerate both string and symbol keys, including non-enumerable ones?

267 views
01

Understand the problem

Question presented to candidate: "If you needed to write a logging Proxy that reports every single own property key an object has, including symbol keys and non-enumerable string keys, how would you implement the ownKeys trap? Why would Object.keys or a plain for...in loop not be good enough here, and what happens if your trap forgets to include a key the target actually has?"

What a strong answer should cover:

  • Object.keys(), for...in, and JSON.stringify only ever see ENUMERABLE STRING keys — they silently skip non-enumerable string keys and skip every symbol key entirely, with no error or warning.
  • Reflect.ownKeys() is the one method that returns everything: every own string key AND every own symbol key, enumerable or not, in the spec-defined order.
  • That order is not insertion order across the board — it is integer-like keys first in ascending numeric order, then remaining string keys in insertion order, then symbol keys in insertion order.
  • A Proxy's ownKeys trap should generally just return Reflect.ownKeys(target) (or something equivalent to it) rather than something ad hoc like Object.keys(target), which would silently under-report.
  • The ownKeys trap has real, spec-enforced invariants: its result must include every non-configurable own key of the target, or a real TypeError is thrown — this is not merely a convention, it is enforced by the engine.
  • A getOwnPropertyDescriptor trap usually needs to be implemented alongside ownKeys (forwarding to Reflect.getOwnPropertyDescriptor), since Object.keys internally calls both traps to filter down to enumerable keys.

Clarifying questions expected:

  • "Does this proxy need to intercept property reads and writes too, or is enumeration the only behavior being customized?" — clarifies whether a minimal handler (just ownKeys + getOwnPropertyDescriptor) is sufficient.
  • "Should the logging happen on every enumeration call, or only once when the proxy is first created?" — a real design question about where the instrumentation actually belongs.

Code / implementation expected: Yes — a real Proxy with an ownKeys trap plus a runnable test proving the real key ordering and a real invariant-violation TypeError.

reflecttrickyreal-world
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 metaprogramming (Proxy/Reflect) interview questions. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every ordering and error message shown below is **real, captu

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSA logging Proxy built on Reflect.ownKeys, plus real proof of key ordering and the ownKeys invariant throwing (run directly)
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 104 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track