CodeZettaInterview Hub

JavaScript · this · Senior · Architecture

You wrap third-party methods with a Proxy or logging decorator. What `this`-related pitfalls should you design for?

Short Interview Answer

Forwarding traps must preserve the original receiver; naive wrappers can break methods that rely on `this` or brand checks.

Detailed Explanation

If you intercept `get` and return a wrapped function that calls `target[prop](...args)` without `.apply(receiver)`, `this` may become wrong. Using `Reflect.get`/`Reflect.apply` with the correct receiver preserves invariants. Some APIs use brand checks (`this` must be a true Array/Map). Bound methods and getters that return functions complicate wrapping. At lead level: define whether wrappers are transparent, document breakage on exotic objects, prefer higher-order wrapping at export boundaries, and add tests for method extraction and inheritance. Also consider that arrows inside the target won't care about your receiver — lexical `this` ignores Proxy receiver tricks.

Interview Tip

Mention Reflect and receiver — signals deep JS competence.

Common Mistake

Wrapping with `(...args) => obj.method(...args)` without ensuring the right `this` for all method types.

Did you know this answer?