Last Updated: February 25, 2016
·
1.682K
· paulbjensen

Shorthand expression of JS objects in CoffeeScript

CoffeeScript has a wonderful little shorthand expression for Javascript objects. Say you have a javascript object that looks like this:

var name = "Paul Jensen";
var age = 27;
var occupation = "Software developer";

var person = {
  name: name,
  age: age,
  occupation: occupation
};

We could express the person object in CoffeeScript like this:

name = "Paul Jensen"
age = 27
occupation = "Software developer"

person = {name,age,occupation}

CoffeeScript is smart enough to recognise the variable names, and use them as the Object's keys.