5 Objects
Objects are presented as JavaScript’s core organizing structure: collections of name-value properties that turn primitives into practical data models for real applications. The chapter explains that, unlike fixed-shape objects in languages such as Java or C++, JavaScript objects are dynamic and can grow or change at runtime. That flexibility makes objects powerful for building up data, mixing in behavior, and adapting to different situations, but it also means engines must work harder to find properties efficiently.
The chapter then breaks down how properties work, distinguishing between data properties that store values directly and accessor properties that run getter and setter code when read or written. Each property carries descriptor attributes that control whether it can be changed, enumerated, or deleted, and these defaults differ depending on how the property is created. Understanding these details helps explain common bugs like silent assignment failures, missing values in loops, or properties that refuse to disappear, and it also clarifies how special objects can behave in surprising ways.
From there, the text expands into prototypes, showing how objects inherit behavior through shared prototype chains rather than copying methods. It explains how property lookup walks the chain, how writes can shadow inherited data, how this is bound at call time to the original receiver, and why classes are mainly a cleaner syntax over the same underlying system. The chapter also introduces arrays, proxies, symbols, and V8’s hidden classes and inline caches, tying them together with practical guidance: create objects consistently, avoid unnecessary shape changes and deletes, and be careful with inheritance and security so your code stays fast, predictable, and safe.
An illustration of the shared prototype relationship between drawer1, drawer2, and drawerPrototype. Each of our drawers reference exactly the same prototype object.
The property lookup algorithm steps.
Objects shapes accumulate as properties are added or modified on the object.
Summary
- Objects are collections of properties, where each property has hidden descriptor attributes controlling writability, enumerability, and configurability
- Data properties store values directly while accessor properties execute getter/setter functions on access
- Property descriptors default to true for object literals but false for Object.defineProperty()
- Objects inherit properties through prototype chains, with lookups traversing [[Prototype]] links until finding the property or reaching null
- Reading a property traverses the prototype chain; writing creates an own property on the object, causing shadowing
- Prototype pollution exploits runtime prototype modification and can be defended against using Object.hasOwn() checks and null-prototype objects.
- The this keyword dynamically binds at call time based on invocation context, not declaration location
- Classes provide syntactic sugar over prototypes, adding truly private fields with the #-prefix and static members shared across instances
- Arrays are specialized objects with numeric indices stored separately from named properties for performance, automatic length tracking, and methods inherited from Array.prototype.
- Proxy objects intercept fundamental operations through trap handlers, enabling validation, logging, and virtual properties
- Symbol properties create collision-free keys that won't conflict with string properties or appear in normal enumeration
- V8 optimizes objects through hidden classes, with consistent shapes enabling inline caches and property access many times faster than dictionary mode
JavaScript in Depth ebook for free