Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7701603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:01:42+00:00 2026-05-31T23:01:42+00:00

I have the following CoffeeScript code: class Person secret = 0 constructor: (@name, @age,

  • 0

I have the following CoffeeScript code:

class Person
  secret = 0
  constructor: (@name, @age, @alive) ->
  inc: -> secret++

Which compiles to the following JavaScript code:

var Person;
Person = (function() {   
    var secret;
    secret = 0;

    function Person(name, age, alive) {
        this.name = name;
        this.age = age;
        this.alive = alive;
    }
    Person.prototype.inc = function() {
        return secret++;
    };
    return Person;
})();

Currently secret is shared between all instances of Person. Is there a way to make secret a private instance variable in CoffeeScript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:01:43+00:00Added an answer on May 31, 2026 at 11:01 pm

    There is no concept of private members in CoffeeScript as there is none in JavaScript. There are local variables, which you do utilize well in your own solution, but while your solution does hide the secret variable from anything outside of a constructor function, it also introduces an overhead of redeclaring the inc method on every instantiation of a class Person.

    A mistake very common in JavaScript community is trying to project inexistent features of other languages on it, which an attempt of mimicing private members is obviously a case of. There’s no such concept in it and thinking deeper you’ll conclude that it would be just unnatural to an extremely loose dynamic environment which JavaScript is.

    So don’t waste your time and performance of your application on implementing inexistent constructs. Just concentrate on solving your problem – not the problems of lacking language features.

    Now ask yourself: what is so hurtful in having all members public?

    Taking everything said into account the ultimate solution would be:

    class Person
      constructor: (@name, @age, @alive) ->
        @secret = 0
      inc: -> @secret++
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following coffeescript: class Vehicles constructor: (@name) -> class Car extends Vehicles
I have the following CoffeeScript code example: class TestClass constructor: () -> @list =
I have the following code inside a class. (It's coffeescript-- and it's for a
I have following Code Block Which I tried to optimize in the Optimized section
I have the following form code: <input data-color=orange id=vote_color_id_2 name=color type=radio value=2 />Orange <input
I get the following error when I have coffeescript files in my assets/javascript folder
I have some coffeescript like the following: class foo: @bar = 'bob loblaw' processRows:
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
Have following listener for keyboard ArrowDown event(it's key code is 40 ): window.onload =
I've got the following code at the top of my CoffeeScript program to reference

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.