O'Reilly logo

CoffeeScript, 2nd Edition by Trevor Burnham

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Inheritance: Classy Prototype Chains

You’ve seen how prototypes allow a set of objects to share common functionality and how CoffeeScript’s classes provide a useful syntax for bundling prototype properties together. And if that were all classes did, they’d be fairly useful. But where classes really shine is when we want to share properties between objects with distinct but related types, a process called inheritance.

JavaScript supports inheritance through something called “prototype chains.” Let’s say that C’s prototype, B, has its own prototype, A. Then we write this:

 c = ​new​ C
 console.log c.flurb()

First, the runtime checks to see whether the particular C instance, c, has a property flurb; if not, it checks C’s prototype (B). If that’s ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required