Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News CoffeeScript 1.7 Released: Adds Chaining Without Parenthesis, Multiline Strings and More

CoffeeScript 1.7 Released: Adds Chaining Without Parenthesis, Multiline Strings and More

Leia em Português

This item in japanese

Lire ce contenu en français

Jeremy Ashkenas has released version 1.7 of CoffeeScript, and with it introduced some highly anticipated changes to the popular JavaScript transpiler.

Version 1.7 includes one of the most popular requests for the language; support for chaining without parenthesis. Prior to the 1.7 releases, if a developer wanted to chain functions, they had to use parenthesis, which are not required for functions in CoffeeScript.

// prior to 1.7 - parenthesis required to chain
$('#element').addClass('active').css({ left: 5 });

// as of 1.7 - no parenthesis
$ '#element'
.addClass 'active'
.css { left: 5 }

This release also introduces proper support for multiline strings. In previous versions of CoffeeScript, herestrings (or string literals) while intended to preserve new lines and whitespace, would ignore the `\` operator which is meant to designate that two strings should be preserved on the same line. As of 1.7, this is fixed, allowing the developer to cleanly format multiline strings in CoffeeScript.

console.log '''The quick brown fox jumped over the \
lazy dog'''

// prior to 1.7 outputs
The quick brown fox jumped \nover the lazy dog

// as of 1.7 now outputs
The quick brown fox jumped over the lazy dog

Expansion has also been added to array destructuring, which had previously been the longest open issue on the CoffeScript repo.

# get the last item in the animals array
animals = [ 'cat', 'dog', 'hippopotamus' ]

# prior to 1.7
hippo = animals[animal.length - 1]

# as of 1.7
[..., hippo] = animals

# ...both of which transpile to...
hippo = animals[animals.length - 1];

New convenient mathematical operators are present as well in the addition. There is the new power operator, floor division, and a modulo operator (returns the remainder of a division operation).

# power
2 ** 2
# transpiles to...
Math.pow(2, 2);

# floor division
2 // 3
#transpiles to...
Math.floor(2 / 3)

# modulo
2 %% 3
#transpiles to...
var __modulo = function(a, b) { return (a % b + +b) % b; };
__modulo(2, 1);

Other enhancements include bringing CoffeeScript in line with Node.js so that its require statement doesn't automatically run every file in a directory, but behaves like Node and only runs the file.

The majority of the work on the 1.7 release (and in fact most of CoffeeScript for the past few years) is done by members of the community. "There are over 100 developers who have contributed to and had patches merged into CoffeeScript" said Jeremy. "Whatever adoption CoffeeScript has enjoyed has happened because the idea appeals to JavaScript programmers." In regards to work on the 1.7 release, Jeremy sent special thanks to Michael Srb for his contributions.

CoffeeScript has indeed enjoyed immense popularity, peaking at one point as the 10th most popular project on GitHub. It's also seen support in frameworks such as Ruby on Rails (since version 3.1), and is supported in Microsoft's Visual Studio via the Web Essentials plugin. Additionally JavaScript creator Brenden Eich has expressed how CoffeeScript influenced his thoughts on the future of JavaScript.

GitHub user stefanpenner noted that in CoffeeScript “…ES6 import export would be killer…”

Jeremy does address ES6 features in CoffeeScript saying,

CoffeeScript is mostly finished — has been quite stable for a couple of years now — but will continue to grow in small ways in the future. Some examples are: support for new JavaScript features as they land, further improved source map support, more polish for the literate programming style and more streamlining for the internals of the compiler.

At one point there, was a Kickstarter project to re-write the CoffeeScript compiler. The project has since been successfully funded and is dubbed CoffeeScriptRedux. Jeremy sees the creation of new compilers as a benefit for CoffeeScript saying, " The more compilers that successfully target a given language — the healthier that language is. It's to CoffeeScript's benefit to have multiple independent compilers."

The 1.7 release is available immediately via GitHub, or the official CoffeeScript site.

Rate this Article