chapter five

5 Objects

 

This chapter covers

  • Controlling property behavior with descriptors
  • Sharing behavior through prototype chains
  • Binding this to objects at call time
  • Intercepting operations with Proxy objects
  • Optimizing performance with V8's hidden classes

There’s a terrible joke that an old lawyer friend always loves telling whenever he gets the chance:

“I Object!”, a defendant screams.The judge, looking concerned, responds, “No… you human.”

I did warn you that it was terrible! But now that we've covered JavaScript's primary set of data type primitives, it's a great way to introduce the subject of the next part of our exploration of JavaScript data types: objects!

Throughout the previous chapters, we've examined JavaScript's primitive types in isolation. Now we start to put these primitives into action in JavaScript's fundamental organizing structure: objects. Every primitive we've studied becomes a building block:

  • Strings and symbols serve as property names and values
  • Numbers, booleans, null, undefined all become property values
  • The type coercion rules from Chapter 4 control how objects convert to primitives for comparison and display.
  • Even the V8 optimization patterns we've seen like SMIs for numbers, pointer tagging for null and undefined, ConsStrings for concatenation, have parallels in how V8 optimizes object property access through hidden classes and inline caches.

5.1 What are objects?

5.2 Properties

5.2.1 Data properties: the simple stuff

5.2.2 Accessor properties: dynamic retrieval

5.2.3 Property descriptors: controlling property behavior

5.3 Prototypical inheritance: the filing cabinet family tree

5.3.1 The Property lookup algorithm: how Javascript finds properties

5.3.2 Navigating the prototype chain

5.4 The mystery of this: context and identity

5.4.1 Binding this

5.4.2 The prototype chain and this

5.5 Functions are just objects

5.6 Modern Javascript: classes

5.6.1 Private fields and methods

5.6.2 Static methods, properties, and initializers

5.7 Arrays

5.8 Proxy objects

5.9 Converting objects to strings and symbol properties

5.10 How V8 implements objects

5.10.1 Hidden classes and shapes: the secret object blueprint

5.10.2 Inline caches: V8's speed boost system

5.11 Summary