JavaScript Spessore
- Length: 340 pages
- Edition: 1
- Language: English
- Publisher: Leanpub
- Publication Date: 2014-06-24
JavaScript Spessore celebrates “Thinking in Objects,” starting with the basics, building upon them, and then exploring new ways to think about object-oriented programming.
0, 1, ∞
To truly think in objects, you have to liberate yourself from thinking in terms of any one language’s features, because there is more than one way to “do” objects and object-oriented programming. Let’s compare a few other languages to JavaScript:
- Smalltalk has objects, but a Smalltalk object’s methods and instance variables are distinct from the contents of a Smalltalk container like a dictionary or array. JavaScript’s objects are dictionaries, and an object’s methods and instance variables are the same thing as its contents.
- Ruby has classes, modules, the metaclasses, and eigenclasses. JavaScript just has objects that are related to each other either with prototype chaining or as instance values.
- When you invoke a method in Common Lisp, You may also be invoking multiple “before,” “after,” or “around” demons in addition to the method handler. In JavaScript, each method is handled by exactly one function.
- Java’s methods cannot be added to or removed from classes once their bytecodes have been loaded. JavaScript’s methods can be added and removed at any time.
These four “distinctions” between other languages and JavaScript are also the four pillars of object-oriented programming language semantics:
- Objects are the things we use to encapsulate data and behaviour by exposing methods (and optionally properties).
- Metaobjects like classes or prototypes are objects that define the behaviour of other objects.
- Protocols are the rules by which we figure out what exactly happens when we send a message to an object.
- Binding Times are the rules that determine when the behaviour of objects, metaobjects, and protocols can be added, removed, or changed.
When we are truly “thinking in objects,” we are thinking in objects, thinking in metaobjects, and thinking in protocols. And for good measure, we are also thinking of when these things are “bound.” And that’s why JavaScript Spessore’s mission is to explore objects, metaobjects, protocols, and to examine the implications of when these behaviours are bound.
J(oop)S
You may be thinking to yourself, “This is all very well, but it sounds like it is about object-oriented programming in general and not really about the specifics in JavaScript. Why JavaScript? Why not Lisp or Smalltalk or OCaml or some other language with more powerful features?”
The answer is that this is a book for programmers about thinking in objects, thinking that works in any OO language. It happens to be written in JavaScript instead of Lisp for the same reason that it happens to be written in English instead of Latin: Because it’s a language we share.
Table of Contents
- Prefaces
- The Big Idea
- JavaScript’s Objects
- Object Recipes
- Instances and Prototypes
- Methods
- Metaobjects
- Encapsulation and Composition
- Inheritance, Ontologies, and Semantic Types
- Metaobject Protocols
- Appendix: Source Code