CHAPTER 6

Using JavaScript Libraries

When developers move to a new language or to a newer version of one they were already using, they usually have to throw away much, if not all, of the code they had previously written. This can be because the syntax is too different or because its libraries use different APIs, and it’s not always practical to attempt to convert code from the old style to the new. With CoffeeScript, though, things work differently: It compiles into JavaScript.

For the developer already using JavaScript, the move to CoffeeScript is much easier, because none of those problems exist. Old code is still usable, so all of the investment in JavaScript is not lost. It’s better than backward compatibility, though—CoffeeScript is also forward compatible! New code can be written in CoffeeScript and integrated into existing JavaScript projects. This chapter shows you how.

Out of necessity, you will find numerous Java­Script examples—don’t let those put you off! You do need to examine some of the dustier corners. However, by the end of the chapter, you will have explored some of the deeper aspects of JavaScript and should have a renewed appreciation for the work the CoffeeScript compiler is doing for you.

Easy calling conventions: don’t throw away that existing code!

Why would a developer mix CoffeeScript and JavaScript code? This is an important question. And the honest answer, from my perspective, is that in general doing so isn’t a good idea. Also, developing a project ...

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.