Overview

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

FAQ

What is an object in JavaScript?An object is a collection of properties, where each property is a name-value pair. Objects let you group related data and behavior into a single structure.
What is the difference between data properties and accessor properties?Data properties store a value directly, while accessor properties use getter and setter functions to compute or modify values when the property is read or written.
What are property descriptors used for?Property descriptors control how a property behaves. They define attributes such as whether a property is writable, enumerable, configurable, or, for accessors, whether it has getter and setter functions.
What do [[Writable]], [[Enumerable]], and [[Configurable]] mean?[[Writable]] controls whether a property’s value can be changed, [[Enumerable]] controls whether it shows up in loops and Object.keys(), and [[Configurable]] controls whether the property can be deleted or redefined.
How do default property descriptor values differ between object literals and Object.defineProperty()?Properties created with object literal syntax default writable, enumerable, and configurable to true. Properties created with Object.defineProperty() default those attributes to false unless you explicitly set them.
How does prototype-based inheritance work?When a property is not found on an object, JavaScript looks for it on the object’s prototype, then the prototype’s prototype, and so on until it finds the property or reaches null. This allows objects to share behavior without copying it.
Why does assigning to a prototype property sometimes create a new property on the object instead?If the inherited property is a data property, assigning to it creates a new shadowing property on the object itself. If the inherited property is an accessor with a setter, the setter is called instead.
What does this refer to in a method call?this refers to the object the method is operating on at call time. When a method is called through an object or inherited through a prototype, this is usually the object that initiated the call, not the prototype where the method was found.
What are Proxy objects used for?Proxy objects let you intercept fundamental operations like property access, assignment, deletion, and enumeration using trap functions. They are useful for validation, logging, virtual properties, and reactive systems.
Why does V8 care about hidden classes and object shapes?V8 uses hidden classes to speed up property access by tracking object structure. Objects with consistent shapes are faster, while objects that change shape often or use delete can fall back to slower property storage and lose performance.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • JavaScript in Depth ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • JavaScript in Depth ebook for free