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