You can extend CoffeeScript classes with ES6 classes
TL;DR: If you want to extend from CoffeeScript written classes, you can use the ES6 class syntax to do so.
Every once in a while you run into a library that was written with the best intentions, but in CoffeeScript. Now you need that library badly, even want to write your own extensions … but alas, the lack of parentheses keeps you from the caffeine. That just happened to me with liquid-node.
Lovely tool, but to extend it to suit my needs, I would have to write in CoffeeScript. Says the documentation. Turns out, you don’t have to. Not if your runtime environment is capable of ES6 classes.
Take this code listing for example:
What’s going on?
- This is the library written in CoffeeScript. It features lots of classes that can be used to create or own functionality.
- With ES6 syntax, we can extend from the class
Liquid.Blocklike it would be done with CoffeeScript
- You can even call the constructor from the class you extend from
- Or do any other
Why does that work
ES6 classes do they same, but baked in your runtime environment. In this case: Node 5. Should you use Babel to transpile ES6 classes to ES5, you will see that the outcome is somewhat similar to the output created by CoffeeScript.
If you want to learn more about prototypical inheritance, I strongly suggest you check out Eric Elliot’s fantastic articles on that topic:
Comments? Shoot me a tweet!