if it's not dark, it's not data

dark overlord of data

Adding Getters and Setters to a Ball of Yarn

30 Decenber 2013

That doesn't sound very useful. But you know what does? Adding getters and setters to a coffee-script class - after all, typescript has them, and I want to have similar functionality in my coffee-script.

Getters and setters are implemented using the Object.defineProperty method. We could implement each property with a call to this method, but I don't want to type a bunch of cruft to define each class property. So lets do it meta - after all, coffee-script IS javascript!


Notice that I've augmented the Function object (:: is coffescript notation for prototype).  This will add a get method to all functions, and in coffeescript a class is a function. We can define set the same way:

But what does that get us? Lets look at an implementation:

Inside of a class definition, but outside of a method scope, @ (this) refers to the class. So we are calling the get method of the class that we defined earlier, to define a getter on the class.

And, if we explicitly invoke the super object, we can inherit the new attributes:
Now aren't we glad we didn't turn into a big ball of yarn!
comments powered by Disqus

more...