CoffeeScript Looping, Objects and Builds, Page 2
Looping with Comprehensions in CoffeeScript
Another interesting CoffeeScript feature is its particular approach to looping statements. In lieu of the traditional for
statement you can use what the CoffeeScript documentation refers to as a comprehension. This syntax is incredibly succinct, allowing you to loop over an array using just one line of code, as demonstrated here:
positions = [
'38.894505, -77.025034',
'38.904483, -77.036048',
'38.897041, -77.023521',
'38.894505, -77.025034'
]
alert position for position in positions
The last line forms the looping statement, declaring that alert(position)
should execute once for every element (stored in position
) in the positions
array. Once compiled, the resulting JavaScript looks like this:
(function() {
var position, positions, _i, _len;
positions = ['38.894505, -77.025034', '38.904483, -77.036048',
'38.897041, -77.023521', '38.894505, -77.025034'];
for (_i = 0, _len = positions.length; _i < _len; _i++) {
position = positions[_i];
alert(position);
}
}).call(this);
Creating Objects in CoffeeScript
CoffeeScript borrows from YAML syntax in a manner which allows you to streamline the creation of objects. All you need to do is indent each property, as demonstrated here:
locations =
embassy:
name: 'Former Soviet embassy'
latitude: 38.904483
longitude: -77.036048
museum:
name: 'International Spy Museum'
latitude: 38.897041
longitude: -77.023521
Compiling this snippet produces the following JavaScript object:
var locations;
locations = {
embassy: {
name: 'Former Soviet embassy',
latitude: 38.904483,
longitude: -77.036048
},
museum: {
name: 'International Spy Museum',
latitude: 38.897041,
longitude: -77.023521
}
};
Having Cake with Your Coffee
Beyond the enormous set of syntactical conveniences, CoffeeScript is even bundled with its own build system which you can use to automate a wide variety of JavaScript-related tasks. Called Cake (not to be confused with CakePHP), you can create build files which define tasks (conveniently written in CoffeeScript) for automating anything which strikes your fancy, such as executing JSLint, running JSUnit unit tests, or building documentation.
Where to From Here?
Although the project has attracted a great deal of interest, CoffeeScript is still in its infancy and learning resources are few and far between. Fortunately, the CoffeeScript documentation is incredibly well done. In particular I suggest reviewing the examples and resources section, which among other things contains links to several impressive projects implemented using CoffeeScript, among them the amazing tank game orona. Additionally, be sure to check out Jamis Buck's amazing CoffeeScript-driven mazes.
Are you currently doing anything with CoffeeScript? If so tell us about it in the comments!
About the Author
Jason Gilmore-- Contributing Editor, PHP--is the founder of EasyPHPWebsites.com, and author of the popular book, "Easy PHP Websites with the Zend Framework". Jason is a cofounder and speaker chair of CodeMash, a nonprofit organization tasked with hosting an annual namesake developer's conference, and was a member of the 2008 MySQL Conference speaker selection board.
Originally published on https://www.developer.com.
Page 2 of 2
This article was originally published on May 19, 2011