Appendix A. ES6 Class
If thereâs any takeaway message from the second half of this book
(Chapters 4-6), itâs that classes are an optional design pattern for
code (not a necessary given), and that furthermore they are often quite
awkward to implement in a [[Prototype]]
language like JavaScript.
This awkwardness is not just about syntax, although thatâs a big part
of it. Chapters 4 and 5 examined quite a bit of syntactic ugliness, from the
verbosity of .prototype
references cluttering the code, to explicit
pseudo-polymorphism (see Chapter 4) when you give methods the same name
at different levels of the chain and try to implement a polymorphic
reference from a lower-level method to a higher-level method.
.constructor
being wrongly interpreted as âwas constructed byâ and yet
being unreliable for that definition is yet another syntactic ugly.
But the problems with class design are much deeper. Chapter 4 points out
that classes in traditional class-oriented languages actually produce a
copy action from parent to child to instance, whereas in
[[Prototype]]
, the action is not a copy, but rather the oppositeâa delegation link.
When compared to the simplicity of OLOO-style code and behavior
delegation (see Chapter 6), which embrace [[Prototype]]
rather than
hide from it, classes stand out as a sore thumb in JS.
class
But we donât need to argue that case again. I remention those issues briefly only so that you keep them fresh in your mind now that we turn our attention to the ...
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.