Using Coffeescript Classes to Extend Libraries

5.15.14
Culture

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 SMASHING BOXES BLOG UPDATES.

Follow Smashing Boxes on TWITTER, LINKEDIN, and FACEBOOK.

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

Smashing Boxes, a leading software development agency in Durham NC, says goodbye to 2021 and HELLO to 2022!
12.1.22

Hello 2022

Culture
Smashing Boxes Chat

Smashing Boxes, a leading software development agency in Durham NC, says goodbye to 2021 and HELLO to 2022!

Steve Banfi and Mike Dalton recently joined the team, amplifying Smashing Boxes’ design offering.
5.8.21

New Members Join the Smashing Boxes Design Team

Culture
Smashing Boxes Chat

Steve Banfi and Mike Dalton recently joined the team, amplifying Smashing Boxes’ design offering.

Agencies’ varying areas of expertise and thought leadership provide many benefits, elevating businesses—from small start-ups to major organizations—to the next level.
4.8.21

What’s an Agency? A Resource to Help Your Business Break Through

Culture
Smashing Boxes Chat

Agencies’ varying areas of expertise and thought leadership provide many benefits, elevating businesses—from small start-ups to major organizations—to the next level.

Let's Make
Something
Together
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
>

Featured on

Upcity partnership logo

top mobile app development company

Good Frims partnership logo
LinkedIn logo iconDribble logo iconTwitter logo icon
L
E
T'
S

C
H
A
T