Proxy & Reflect: Advanced JavaScript Interviews
JS جاوااسکریپت

Proxy & Reflect: Advanced JavaScript Interviews

Intercept property access, validate objects, and implement reactive patterns like Vue 3's reactivity.

BugCast Admin··۵ دقیقه مطالعه

Proxy wraps an object and intercepts fundamental operations. Reflect provides methods that mirror Proxy traps with correct this binding.

Common Interview Questions

Q: What is a Proxy trap?

Answer: A handler method like get, set, has, or deleteProperty that runs when the corresponding operation occurs on the proxy.


const user = { name: "Sam" };
const proxy = new Proxy(user, {
  get(target, prop) {
    return prop in target ? target[prop] : "N/A";
  },
});

Q: Why use Reflect inside traps?

Answer: Reflect methods forward to the target with correct semantics and return booleans for operations like Reflect.set instead of throwing in strict mode edge cases.

Q: Name a real-world Proxy use case

Answer: Validation layers, default values, immutable wrappers, logging/debugging, and reactive state systems that track dependency reads/writes.

Q: Proxy limitations?

Answer: Cannot proxy non-configurable/non-writable properties freely, private class fields are not trapped the same way, and some performance overhead exists on hot paths.

Key Points for Interviews

  • Proxy enables meta-programming at runtime

  • Always prefer Reflect in traps for forward compatibility

  • Vue 3 reactivity uses Proxy instead of Object.defineProperty

  • Proxies can make debugging harder — use deliberately

  • Revocable proxies via Proxy.revocable cut off access

#proxy#reflect#interview

مطالب مرتبط