Part 06 · Objects · 6.1.04

Properties, descriptors, prototype chain và class syntax

Property lookup đi qua prototype chain; class là syntax/runtime semantics trên prototype model, không biến JavaScript thành Java.

Security boundary: object merge/parsing nhận input chưa tin cậy phải kiểm soát keys/prototypes; prototype pollution có thể biến một utility innocuous thành global behavior change.

1. Properties và descriptors

Own property key là String hoặc Symbol. Data descriptor có value/writable; accessor descriptor có get/set; enumerable/configurable quyết định iteration và redefinition. Spread và Object.assign chỉ shallow-copy enumerable own values nhưng có getter/setter invocation semantics khác nhau.

Object.getOwnPropertyDescriptor(obj, "value")
Object.hasOwn(obj, key) // không đi qua prototype

2. Prototype lookup và assignment

Lookup tìm own property rồi đi qua [[Prototype]] chain. Assignment có rules với inherited setter/writable data property; đọc được property không có nghĩa ghi được property. Prototype mutation làm engine optimization khó và input keys đặc biệt có thể gây prototype pollution tại merge/parser boundary.

OperationQuestion cần trả lời
ReadOwn hay inherited? Getter có side effect không?
WriteSetter/inherited writable property hay tạo own property?
EnumerateEnumerable own/inherited và String/Symbol key nào?
MergeDeep hay shallow, key allow-list, prototype pollution guard?

3. Classes và private state

Methods nằm trên prototype, fields theo instance; static members nằm trên constructor. Private fields dùng branded access và không phải string key có thể enumerate. extends/super có initialization constraints; class method mặc định strict.

4. Immutability và state updates

const chỉ khóa binding, không freeze object. Object.freeze shallow; nested object vẫn mutable nếu không recursively control. Immutable update rõ ownership/state boundary nhưng có allocation cost; Map/Set cần clone hoặc controlled mutation.

Interview answer: “I distinguish own/inherited properties and descriptor behavior before choosing a copy or merge. I treat prototypes as a security and performance boundary, not just inheritance syntax.”
Tài liệu: MDN Prototype Chain · MDN Classes · MDN Prototype Pollution