Chapter 4
Classes, Prototypes, and Inheritance

In the previous chapter, you learned to create and manipulate objects. But we don’t yet have a way of defining types of objects. In JavaScript, types are described by prototypes, which define methods (and, less often, data) that can be shared across many objects. For that reason, JavaScript is sometimes described as a prototype-based language.

This dynamic approach to sharing methods is powerful but has a cost in clarity. If you’re reading code in a strictly class-based language like C++ or Java and you want to find out which methods a particular object supports, you just have to look at the code that defines that object’s class. But if you want to know which methods a JavaScript object has (without running the code), you have to track down every possible reference to that object or its prototype anywhere in the application.

Several approaches have popped up over the years for organizing JavaScript code into something resembling classes. Over time, a standard pattern evolved. That pattern provides the basis for CoffeeScript’s classes, which preserve the dynamism of prototypes while greatly simplifying the task of defining new types of objects. In this chapter, we’ll review the mechanics of JavaScript prototypes, then meet CoffeeScript’s two most powerful keywords: class and extends.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.19.243