r/programming • • Jul 25 '13

CoffeeScript's Scoping is Madness

http://donatstudios.com/CoffeeScript-Madness
207 Upvotes

315 comments sorted by

View all comments

Show parent comment

2

u/rwbarton Jul 26 '13

Apparently like this:

counter1 = (->
  do (i = 0) ->
    -> i++)()

3

u/iopq Jul 26 '13

Aren't you just binding it to the parameter inside your inner function? So instead of just adding a var, you declared a function with a parameter i to it.

This is what it compiles to:

var counter1;

counter1 = (function() {
  return (function(i) {
    return function() {
      return i++;
    };
  })(0);
})();

2

u/rwbarton Jul 26 '13

Yes. That code is equivalent to

var counter1;

counter1 = (function() {
  var i = 0;
  return function() {
    return i++;
  };
})();

which is what you wanted, I think. As far as I know you cannot write CoffeeScript that compiles to the above javascript exactly.