Hard Rock CoffeeScript

Object literals

Object literals can be specified exactly as in JavaScript, with a pair of braces and key/value statements. However, like with function invocation, CoffeeScript makes the braces optional. In fact, you can also use indentation and new lines instead of comma separation.

rockStar = {tallent: '30%', charisma: '70%'}

rockStar = tallent: '30%', charisma: '70%'

rockStar =
  tallent: '30%'
  charisma: '70%'

Arrays

Arrays definitions look pretty similar to object literals - coma separated if defined in a single line and you can use whitespace instead of comma separators, although the square brackets [] are still required.

nirvana = ['Bleach', 'Nevermind', 'Incesticide', 'In Utero']

nirvana = [
  'Bleach'
  'Nevermind'
  'Incesticide'
  'In Utero'
]

CoffeeScript also strippes the trailing comma in arrays which is another common source of cross-browser errors.

Ranges

CoffeeScript takes inspiration from Ruby and provides you an ability to define ranges if you need an array of sequential integers. Ranges are created by two numerical values, the first and last positions in the range, separated by .. or .... With two dots (1..4), the range is inclusive (1, 2, 3, 4); with three dots (1...4), the range excludes the end (1, 2, 3).

CoffeeScript

rangeOne = [1..10]
rangeTwo = [1...10]

JavaScript

var rangeOne, rangeTwo;
rangeOne = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
rangeTwo = [1, 2, 3, 4, 5, 6, 7, 8, 9];

If, however, the range is specified immediately after a variable, CoffeeScript converts it into a slice() method call.

numbers = [1, 2, 3, 4][0..1] # => [1, 2]

In the example above, the range returns a new array, containing only the first two elements of the original array. You can also use the same syntax for replacing an array segment with another array.

CoffeeScript

numbers = [0..9]
numbers[3..5] = [-3, -4, -5] # => [0, 1, 2, -3, -4, -5, 6, 7, 8, 9]

JavaScript

var numbers, _ref;
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
[].splice.apply(numbers, [3, 3].concat(_ref = [-3, -4, -5])), _ref;

What's neat, is that JavaScript allows you to call slice() on strings too, so you can use ranges with string to return a new subset of characters.

CoffeeScript

lyrics = "So close no matter how far"[0..7] # => "So close"
Яндекс.Метрика