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/[deleted] Jul 26 '13

You can write this in Coffeescript:

class MyClass
  test: ()->
    _private_method()
  _private_method = () ->
    console.log "this is a private method"

(note the subtle difference between "_private_method:" and "_private_method ="). Then the compiler will generate:

// Generated by CoffeeScript 1.5.0
var MyClass;

MyClass = (function() {
  var _private_method;

  function MyClass() {}

  MyClass.prototype.test = function() {
    return _private_method();
  };

  _private_method = function() {
    return console.log("this is a private method");
  };

  return MyClass;

})();

1

u/drunken_thor Jul 26 '13

Ah! So it does, I just tried it out and it compiled just fine. Why have I not seen this specifie before because that would be a perfect definition of a private method then rather than all these other suggestions I am getting.

Thanks for taking the time to teach me something!