External Libraries

Using external libraries is exactly the same as calling functions on CoffeeScript libraries because, at the end of the day, everything is compiled down to JavaScript. Using CoffeeScript with jQuery is especially elegant, due to the amount of callbacks in jQuery’s API:

# Use local alias
$ = jQuery

$ ->
  # DOMContentLoaded
  $(".el").click ->
    alert("Clicked!")

Since all of CoffeeScript’s output is wrapped in an anonymous function, we can set a local $ alias for jQuery. This will make sure that even if jQuery’s no conflict mode is enabled and the $ re-defined, our script will still function as intended.

Get The Little Book on 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.