Chapter 6. Behavior Delegation
In Chapter 5, we addressed the [[Prototype]]
mechanism in
detail, and why itâs confusing and inappropriate (despite countless
attempts for nearly two decades) to describe it in the context of âclassâ or
âinheritance.â We trudged through not only the fairly verbose syntax
(.prototype
littering the code), but the various gotchas (like
surprising .constructor
resolution or ugly pseudopolymorphic syntax).
We explored variations of the âmixinâ approach, which many people use to
attempt to smooth over such rough areas.
Itâs a common reaction at this point to wonder why it has to be so complex to do something seemingly so simple. Now that weâve pulled back the curtain and seen just how dirty it all gets, itâs not a surprise that most JS developers never dive this deep, and instead relegate such mess to a âclassâ library to handle it for them.
I hope by now youâre not content to just gloss over and leave such
details to a âblack boxâ library. Letâs now dig into how we could and
should be thinking about the object [[Prototype]]
mechanism in JS, in
a much simpler and more straightforward way than the confusion of
classes.
As a brief review of our conclusions from Chapter 5, the [[Prototype]]
mechanism is an internal link that exists on one object that references
another object.
This linkage is exercised when a property/method reference is made
against the first object, and no such property/method exists. In that
case, the [[Prototype]] ...
Get You Don't Know JS: this & Object Prototypes now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.