Using Coffeescript Classes to Extend Libraries

Coffeescript classes provide an easy mechanism for creating child classes of anything that uses the prototypical inheritance pattern. The ‘extends‘ keyword creates a child object with it’s prototype being the class methods you define. For example:

class InfoWindow extends google.maps.InfoWindowNew instances of InfoWindow will have a prototype object containing the methods and properties of your class. That prototype’s prototype will be the prototype of the object you are extending (i.e. google.maps.InfoWindow.prototype). This allows you to augment google.maps.InfoWindow and even write methods of the same name on your child class without clobberring the parent. To access the parent’s method of the same name, coffeescript provides the keyword ‘super‘. For example: class InfoWindow extends google.maps.InfoWindow   constructor: (@_template, @_map) ->   open: (context) -     super @_map, context   update: ->     @setContent @_template.innerHTMLThose familiar with google maps API will know that google.maps.InfoWindow already has a .open() method. I want to augment what that method does but I don’t want to overwrite the method itself. To accomplish this, I extend google.maps.InfoWindow into my own class, define an ‘open‘ method, and have it wrap the native one by using the ‘super‘ keyword. This allows me to abstract away the first parameter (instance variable @_map) so I do not need to supply it each time I call .open().This ability to extend classes provides a clean and simple way to augment libraries you use and write code at a higher level. Happy extending!Smashing Boxes is a web and mobile app development company know for creating a lasting experience through bold design and disrupting the status quo. We are entrepreneurs and craftsmen first, and a digital creative agency second. Inspired by our visionary clients, we transform ideas into innovative web and mobile applications. Take a look at our work.Cover photo by Justin Leibow

Subscribe to the Smashing Boxes Blog today!

Smashing Boxes is a creative technology lab that partners with clients, taking an integrated approach to solving complex business problems. We fuse strategy, design, and engineering with a steady dose of entrepreneurial acumen and just the right amount of disruptive zeal. Simply put, no matter what we do, we strive to do it boldly. Let's talk today!

Related Posts

The Shifting Landscape of Seed-Stage Startups: Insights from Carta

The seed-stage startup landscape is evolving, with shifts in deal volume and geographic distribution. The data provided by Carta offers valuable insights into these changes.

view post

Why Everyone Needs a Lab

Now through July 18th, you can see a digital art installation in the first-floor window of the 21c Hotel

view post