More technology related reviewing next, JavaScript Patterns by Stoyan Stefanov. This is part of my continuing effort to learn JavaScript.
For me this isn’t a question of learning the nuts and bolts of a language but rather one of learning to use it fluently and idiomatically.
I thought this book might be in the spirit of the original “Gang of four” design patterns, but although it mentions these design patterns it is more generally about good style in JavaScript. The book is divided into eight chapters including an introduction.
The first substantive chapter on “essentials” talks mainly about variable declarations and some odds and ends. The most interesting one of these was the behaviour of parseInt which converts a string into an integer. Except if the string starts with a zero, as ISO8601 days and months would, then parseInt assumes it is a number in base 8 (octal)!! I can foresee many long hours trying to debug this problem without this forewarning. This chapter also discusses the importance of coding style conventions.
The second chapter talks about literals and constructors. It strikes me that much of this is about unwinding the behaviour of developers more used to statically-typed languages. The JavaScript way is to create objects by example, rather than take a class definition and derive from that. Although in the permissive manner of many languages it will let you do it either way. Since this book was written JavaScript has gained a “class” keyword which allows you to construct classes as you might in Java or C#.
Next up are functions, JavaScript shares Python’s view of functions as objects, allowing them to be passed as arguments. This is particularly important in JavaScript to provide “callback” functionality which is very useful when doing asynchronous programming. I learn here that the “currying” of function is named after Haskell Curry, who also has a whole language named for him. I always feel when passing functions as arguments that I am fiddling with the underpinnings of reality – it can make debugger difficult too.
I found the idea of functions that redefine themselves on first run interesting, it sounds useful and dangerous at the same time.
The chapter on object creation patterns is all about introducing module like behaviour and namespacing to JavaScript which at the time the book was written were not part of the language. Also covered are making private properties by hiding them in function closures.
The code reuse chapter is largely about patterns for achieving inheritance-like behaviour. This introduces a range of patterns which build up to almost exactly replicate class-based inheritance.
Finally we meet some of the classic Gang of Four design patterns. Some of these patterns, such as the iterator pattern, have been absorbed entirely into the core of languages like Python and more recently, JavaScript. The Observer patterns is implemented in web browsers as events, which are ubiquitous. Perhaps the lesson of this chapter is that some of the Gang of Four patterns have been absorbed into the core of languages, we use them almost without thinking. The Strategy Pattern, which determines algorithms at runtime, fits well with the chapter on functions and JavaScript’s view of functions as objects.
The book finishes with a chapter on patterns for the Document Object Model, or rather JavaScript in the browser. It includes well-known advice such as not testing for browser type but rather testing for functionality. It also has advice on optimising JavaScript for deployment.
There is minimal mention of specific tools or libraries in this regard, although Yahoo’s YUI library is mentioned a few times – Stefanov has worked on this library so this is unsurprising, and not unreasonable.
This book had more of the air of Douglas Crockfords’ JavaScript: The Good Parts than a book on patterns which was what I was expecting. Alternatively perhaps “JavaScript for users of statically-typed languages”, as such it probably works pretty well for Python programmers too although modules have always been built-in to Python and there is a “class” keyword for specifying classes.
JavaScript Patterns is readable though, I’m glad I picked it up.