Problem, as an interviewer would state it:
"Write withHooks(obj, { before, after }) — wrap EVERY method on obj so that a before hook fires just before each call, and an after hook fires just after, WITHOUT altering the method's own real behavior or return value."
Examples:
const wrapped = withHooks(calculator, { before: logCall, after: logResult });
wrapped.add(2, 3); // logs before, runs add(2,3) normally, logs after, returns 5 unchanged
Clarifying questions expected:
- Should non-function properties on the object be left untouched, or is this only for methods?
- Does the wrapped method need to preserve the original
thisbinding when called? - Should this handle an async method's hooks firing correctly relative to when the promise actually settles, not just when it is initially called?
Code / implementation expected: Yes — real, direct proof that both hooks fire correctly around real method calls, with return values passed through completely unchanged.