Free Chapter from Douglas Crockford's "JavaScript: The Good Parts"

By YUI TeamMay 5, 2008

Click through to read Ch. 5 of Douglas's forthcoming book.The #2 book on Amazon's JavaScript shelf this morning is Douglas Crockford's forthcoming Javascript: The Good Parts (currently available for preorder and due for release later this month). In this volume, Douglas articulates a philosophy of coding in JavaScript that builds up on and extends the content of his popular video series and articles. The premise, as Douglas puts it: "Most programming languages contain good parts and bad parts. I discovered that I could be a better programmer by using only the good parts and avoiding the bad parts."

Douglas and his editors at O'Reilly were kind enought to let us offer a sneak preview of JavaScript: The Good Parts here:

Here's how Douglas opens the discussion of inheritance in JavaScript:

Inheritance is an important topic in most programming languages.

In the classical languages (such as Java), inheritance (or extends) provides two useful services. First, it is a form of code reuse. If a new class is mostly similar to an existing class, you only have to specify the differences. Patterns of code reuse are extremely important because they have the potential to significantly reduce the cost of software development. The other benefit of classical inheritance is that it includes the specification of a system of types. This mostly frees the programmer from having to write explicit casting operations, which is a very good thing because when casting, the safety benefits of a type system are lost.

JavaScript, being a loosely typed language, never casts. The lineage of an object is irrelevant. What matters about an object is what it can do, not what it is descended from.

JavaScript provides a much richer set of code reuse patterns. It can ape the classical pattern, but it also supports other patterns that are more expressive. The set of possible inheritance patterns in JavaScript is vast. In this chapter, we'll look at a few of the most straightforward patterns. Much more complicated constructions are possible, but it is usually best to keep it simple.

Click here to keep reading (1.2MB; PDF).

Thanks to Laurel Ackerman and Simon St. Laurent at O'Reilly for the permission to share the sample chapter.