JavaScript Spessore: A thick shot of objects, metaobjects, & protocols (2014)
Prefaces
Taking a page out of LiSP
Teaching Lisp by implementing Lisp is a long-standing tradition. If you set out to learn to program with Lisp, you will find read book after book, lecture after lecture, and blog post after blog post, all explaining how to implement Lisp in Lisp. Christian Queinnec’s Lisp in Small Pieces is particularly notable, not just implementing a Lisp in Lisp, but covering a wide range of different semantics within Lisp.
Lisp in Small Pieces’s approach is to introduce a feature of Lisp, then develop an implementation. The book covers Lisp-1 vs. Lisp-22, then discusses how to implement namespaces, building a simple Lisp-1 and a simple Lisp-2. Another chapter discusses scoping, and again you build interpreters for dynamic and block scoped Lisps.
Building interpreters (and eventually compilers) may seem esoteric compared to tutorials demonstrating how to build a blogging engine, but there’s a method to this madness. If you implement block scoping in a “toy” language, you gain a deep understanding of how closures really work in any language. If you write a Lisp that rewrites function calls in Continuation Passing Style, you can’t help but feel comfortable using JavaScript callbacks in Node.js.
Implementing a language feature teaches you a tremendous amount about how the feature works in a relatively short amount of time. And that goes double for implementing variations on the same feature–like dynamic vs block scoping or single vs multiple namespaces.
j(oop)s
In this book, we are going to implement a variety of object-oriented programming language semantics, in JavaScript. We will implement different object semantics, implement different kinds of metaobjects, and implement different kinds of method protocols.
We’ll see how to use JavaScript’s basic building blocks to implement things like private state, multiple inheritence, protected methods, and more.
Unlike other books and tutorials, we won’t focus on how to write object-oriented programs. We won’t worry about patterns like “Facade,” or walk through an “extract method” refactoring. We’ll trust that there are more than enough existing resources covering these topics, and focus instead on the areas generally given short shrift by existing texts.
Our approach will be to focus on implementing OOP’s basic tools. This will teach us (1) A great deal about how features like delegation and traits actually work, and (2) How to implement them in languages (like JavaScript) that do not provide much more than cursory support for OOP.
JavaScript Allongé and allong.es
JavaScript Spessore is written for the reader who has read JavaScript Allongé or has equivalent experience with JavaScript, especially as it pertains to functions, closures, and prototypes.
“This is a must-read for any developer who wants to know Javascript better… Reg has a way of explaining things in a way that connected the dots for me. This is probably the only programming book I’ve re-read cover to cover a dozen times or more.”–etrinh
“I think it’s one of the best tech books I’ve read since Sedgewick’s Algorithms in C.”–Andrey Sidorov
“Your explanation of closures in JavaScript Allongé is the best I’ve read.”–Emehrkay
“It’s a different approach to JavaScript than you’ll find in most other places and shines a light on some of the more elegant parts of JavaScript the language.”–@jeremymorrell
Even if you know the material, you may want to read JavaScript Allongé to familiarize yourself its approach to functional combinators. You can read it for free online.
allong.es
allong.es is a JavaScript library inspired by JavaScript Allongé. It contains many utility functions that are used in JavaScript Spessore’s examples, such as map, variadic and tap. It’s free, and you can even type a lot of the examples from this book into its try allong.es page and see them work.