Objects, prototypes and classes · mid
Property descriptors, getters, setters, freeze and seal
Every property has a descriptor — either a data descriptor (value, writable) or an accessor descriptor (get, set) — plus enumerable and configurable flags, and freeze, seal and preventExtensions use these flags to lock an object shallowly.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Object.getOwnPropertyDescriptor(obj, 'k') returns { value, writable, enumerable, configurable } or { get, set, enumerable, configurable }.
- Properties made by assignment or a literal have every flag true. With Object.defineProperty, any flag you leave out defaults to false.
- writable: false -> assignment is silently ignored in sloppy mode and throws TypeError in strict mode.
- enumerable: false -> hidden from for...in, Object.keys, JSON.stringify and spread, but still readable.
- configurable: false -> the property can't be deleted and its descriptor can't be changed (except writable true -> false).
- Getters and setters: computed values, validation, lazy values. JSON.stringify calls getters and writes their values.
- Object.preventExtensions: no new properties. Object.seal: also no deleting (configurable false). Object.freeze: also no writing (writable false). All three are shallow.
The red pen
Common traps: mistake → fix
Mistake: Object.freeze makes an object deeply immutable
Fix: nested objects stay mutable.
Mistake: Writing to a frozen object always throws
Fix: only in strict mode; sloppy mode ignores it silently.
Mistake: defineProperty defaults to writable, enumerable and configurable true
Fix: they default to false.
Easy to hard
Questions interviewers ask, easy to hard
- What are property descriptors?
- Compare freeze, seal and preventExtensions.
- How do you make a read-only property? What happens on write in strict vs sloppy mode?
- Give real use cases for getters and setters.
- Implement deepFreeze. Why do Redux Toolkit and Immer freeze state in development?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.