Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Private methods for CoffeeScript classes #651

devongovett opened this Issue · 4 comments

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) { = _a;
        return this;

    Test.prototype.greeting = function() {

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


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() #error, private method

Which would compile into the following JavaScript:

var OverwriteTest;
(function() {

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

    //private methods = function() {
        return ("Hello " + ( + "!");


Since the private version of foo was last, it would be the one created. If someone tried to call 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?


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.


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.


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:


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
Something went wrong with that request. Please try again.