Coffeescript versus Javascript example for FUN

Coffeescript:

window.someList = [1, 2, 3, 4, 5]

addOne = (item) ->
  1 + item

window.runThings = () ->
  addOne item for item in someList

Coffeescript compiled to JavaScript:

(function() {
  var addOne;
  window.someList = [1, 2, 3, 4, 5];
  addOne = function(item) {
    return 1 + item;
  };
  window.runThings = function() {
    var item, _i, _len, _results;
    _results = [];
    for (_i = 0, _len = someList.length; _i < _len; _i++) {
      item = someList[_i];
      _results.push(addOne(item));
    }
    return _results;
  };
}).call(this);

One thought on “Coffeescript versus Javascript example for FUN”

  1. the JS will look less nasty if you give “runThings” a static return value like “true” … 

    The JS is trying to keep track of the results of each add operation to return.

Comments are closed.