CHAPTER 5

Object-Oriented Design

If ever a few words could strike fear into the heart of web developers, they would be “object-oriented programming” (OOP). With online coding traditionally seen as more of a scripting endeavor, the application of OOP principles has been deemed as best applied on the server-side, where all the serious coding happens.

As AJAX-based applications have become more common, though, the client-side has become increasingly more complex, requiring more thoughtful design on the part of the developer, and OOP plays an important part there. It’s too much to say that all your code should be written in an OOP style, but it’s definitely true to say that OOP should be a tool in the box, and when designing a large application, or one likely to have a substantial lifespan, it’s usually the best tool.

In this chapter, I’ll demonstrate the various features CoffeeScript offers for object-oriented programming, and discuss some of the best practices for designing classes. Since there is a JavaScript runtime underneath, the implementation of objects is flexible and there are opportunities to extend code with features not natively supported in the language, like mixins, and you’ll take a look at those, too.

Classes and inheritance

Although making use of libraries such as jQuery to write code is straightforward, it becomes clear quickly to even a novice that writing procedural code—a series of bare statements on the page—doesn’t get you very far. The first tool in the toolbox ...

Get Smashing CoffeeScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.