Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Private methods for CoffeeScript classes #651

devongovett opened this Issue Aug 27, 2010 · 4 comments


None yet
5 participants

It would be nice to have private methods in CoffeeScript's classes. I am proposing a syntax similar to Ruby's. Just to be clear, the difference between private and protected methods is that protected methods are available to subclasses whereas private methods are not. As I see it, protected methods would be pretty hard to implement without an interpreter, but I think that private methods would be doable and perfectly useful.

The syntax I am proposing for private methods would look like the following.

class Test
    constructor: (@name) ->
    greeting: ->

        internalMethod: ->
            "Hello #{@name}!"

test = new Test("Devon")
alert test.internalMethod() #error, test.internalMethod is not defined
alert test.greeting() #alerts Hello Devon!

All of the methods within the private block would only be accessible within other methods of the class, both private and public.

Here is how that example would compile into JavaScript.

var Test;
(function() {

    var __private = {};
    Test = function(_a) {
        this.name = _a;
        return this;

    Test.prototype.greeting = function() {
        return __private.internalMethod.call(this);

    //private methods
    __private.internalMethod = function() {
        return ("Hello " + (this.name) + "!");


If someone defined both a public and a private version of the method, the last one would win just like it does now. The compiler would need to keep track of this for the generated code. For example:

class OverwriteTest
    foo: ->

        foo: ->

ot = new OverwriteTest()
ot.foo() #error, private method

Which would compile into the following JavaScript:

var OverwriteTest;
(function() {

    var __private = {};
    OverwriteTest = function() {}

    //private methods
    __private.foo = function() {
        return ("Hello " + (this.name) + "!");


Since the private version of foo was last, it would be the one created. If someone tried to call OverwriteTest.foo a JavaScript error would be thrown because the method does not exist in the public space.

What do you think? Would this be a valuable addition to CoffeeScript?


StanAngeloff commented Aug 27, 2010

One big issue I see with this is having to track and rewrite all private calls. There is going to be too much magic in the core happening for my liking.


michaelficarra commented Aug 27, 2010

I agree with StanAngeloff's aversion to this suggestion. It's too complicated of a solution. Similar proposals related to class bodies are being made over in issue #640 right now, if you want to contribute to that.


jashkenas commented Aug 28, 2010

Yep, full-blown private methods (inheritable, overridable), are not possible in JavaScript without sacrificing a little more than we're willing to give.

This is a duplicate of several previous tickets, including this one: http://github.com/jashkenas/coffee-script/issues/issue/222

I actually quite like your proposal. Tracking whether a given call is private seems no more difficult or magic to me than the existing mechanism of tracking local vars, and moving them to the top of functions.

Any idea how private instance variables could be handled?

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment