An Introduction to CoffeeScript
A while ago I started a series about how to brand SharePoint sites using the Yeoman web development workflow. With this small introduction to CoffeeScript I’d like to continue the series.
- Yeoman the web development workflow
- An Introduction to Jade
- An Introduction to CoffeeScript
- An Introduction to SASS
- Our yeoman generators
- Hands On – Let’s create a custom MasterPage
- Hands On – Let’s create a custom PageLayout
- A forecast – What are we currently working on
For most SharePoint developers out there languages such as CoffeeScript are a little bit strange, which is - in my opinion - caused by Microsoft’s developer marketing who did their best to bring Windows developers to the web - but this is a different and sad story (think about ASP.NET WebForms Designer and UpdatePanel..).
So, first the question is why!
- The compile time check is an additional quality-gate
- You’ve to write less code
- It makes dealing with the current context easy (forget about var that = this )
- There are tons of great frameworks, tools and libraries available
- It makes fun writing CoffeeScript
Instead of writing more and more text I’m going to show you some CoffeeScript Code, which should give you a lot of reasons why to learn CoffeeScript.
Defining Arrays and Objects
As you can see in this very easy snippet, you can save a lot of keystrokes because CoffeeScript pays attention to the indentions you're using within your code. This makes thinks like brackets and semicolons unnecessary.
Based on the Ruby syntax you can use cool comparison expressions in CoffeeScript
Iterating in CoffeeScripts
Classes and Context in CoffeeScripts
Define Classes and use inheritance in the easy way. Don't care about prototype inheritance when writing your code. CoffeeScript handles this behind the scenes.
See also the fat arrow (
=>) which is responsible for ensuring that context within the function is the one you've defined at programming time. So you can finally forget all these var that = this statements
CSOM in CoffeeScripts
As you can see CoffeeScript is awesome. Give yourself 30 minutes and checkout coffeescript.org and start writing client-side-code the robust way. In addition to all these benefits it's fun to write CoffeeScript because you can use cool syntactic features from Ruby which will save a lot of time in your daily business.