JavaScript Decorators and Metaprogramming Quiz
Wrap methods with cross-cutting concerns: a read-only decorator, a method-timing decorator, and the stage-3 decorator proposal that landed in TC39.
Question Bank
Hard
JavaScript
quiz
js-proxy-reflect
design-patterns
interview-prep
1,008 views
24
Write a readOnly legacy decorator (TypeScript/Babel experimentalDecorators shape) that takes (target, key, descriptor) and makes the decorated method non-writable.
Examples
Example 1:
Input: class Example { @readOnly method() { console.log('hi'); } } const e = new Example(); e.method = () => {};
Output: TypeError in strict mode; silent no-op in sloppy mode.
Explanation: The decorator flipped `descriptor.writable` to false; assignments to the property are now rejected.2 more questions, with full solutions and explanations, are available for premium members.
Upgrade to Premium