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 Queinnecs 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 Piecess approach is to introduce a feature of Lisp, then develop an implementation. The book covers Lisp-1 vs. Lisp-2, 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 theres 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 cant 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 featurelike 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.
Well see how to use JavaScripts basic building blocks to implement things like private state, multiple inheritence, protected methods, and more.
Unlike other books and tutorials, we wont focus on how to write object-oriented programs. We wont worry about patterns like Facade, or walk through an extract method refactoring. Well 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 OOPs 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 Ive re-read cover to cover a dozen times or more.etrinh
I think its one of the best tech books Ive read since Sedgewicks Algorithms in C.Andrey Sidorov
Your explanation of closures in JavaScript Allong is the best Ive read.Emehrkay
Its a different approach to JavaScript than youll 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 Spessores examples, such as map
, variadic
and tap
. Its free, and you can even type a lot of the examples from this book into its try allong.es page and see them work.
The Big Idea
Detail of a Mazzer Mini espresso grinder. The grind is at least as important as the press, yet most people spend more time and money on their espresso machine than they do on their grinder.
In This Chapter
In this chapter, we propose that JavaScripts Big Idea is that you can use functions to transform and compose primitives, functions, and objects. We will suggest that programming with objects is not separate and distinct from programming with functions, but simply another way to work with JavaScripts big idea.
This thinking will set the stage for the object-oriented programming techniques well explore throughout the book.
Is JavaScript Functional or Object-Oriented?
One of JavaScripts defining characteristics is its treatment of functions as first-class values. Like numbers, strings, and other kinds of objects, references to functions can be passed as arguments to functions, returned as the result from functions, bound to variables, and generally treated like any other value.
Heres an example of passing a reference to a function around. This simple array-backed stack has an undo
function. It works by creating a function representing the action of undoing the last update, and then pushing that onto a stack of actions to be undone:
var
stack
=
{
array
:
[],
undoStack
:
[],
push
:
function
(
value
)
{
this
.
undoStack
.
push
(
function
()
{
this
.
array
.
pop
();
});
return
this
.
array
.
push
(
value
);
},
pop
:
function
()
{
var
popped
=
this
.
array
.
pop
();
this
.
undoStack
.
push
(
function
()
{
this
.
array
.
push
(
popped
);
});
return
popped
;
},
isEmpty
:
function