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 7533929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:52:12+00:00 2026-05-30T05:52:12+00:00

I’m not quite sure the uses for the different variables in CoffeeScript class Cow

  • 0

I’m not quite sure the uses for the different variables in CoffeeScript

class Cow
  @utters = 1

  constructor: (@name) ->

  mutate:->
     alert @utters

  heads: 1
  feet = 9


c = new Cow 

From my investigation, it seems heads is public and feet is private. My confusion comes in when figuring out name and utters. For name it more or less compiles to this.name = name and for utters it compiles to Cow.utters = 1.

So my questions are. What is the scope of utters and how should it be accessed? What is the scope of name and how should it be accessed?

  • 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-30T05:52:14+00:00Added an answer on May 30, 2026 at 5:52 am

    Let us look at these one by one.

    For the first one:

    class Cow
      @utters = 1
    

    this is the class itself when you hit @utters = 1 so this @utters is sort of a class variable. The JavaScript version is this:

    var Cow = (function() {
      function Cow() {}
      Cow.utters = 1;
      return Cow;
    })();
    

    Subclasses will be able to see this but they’ll have their own copy of it; so, for this:

    class CowCow extends Cow
      m: ->
        CowCow.utters = 11
    

    CowCow.utters starts out as 1 but will be 11 after (new CowCow).m() and Cow.utters will stay at 1 all the way through.

    The second one:

    class Cow
      heads: 1
    

    is essentially a default instance variable; the JavaScript version looks like this:

    var Cow = (function() {
      function Cow() {}
      Cow.prototype.heads = 1;
      return Cow;
    })();
    

    The Cow.prototype.heads = 1; part means that heads is inherited and attached to instances rather than classes.

    The result is that this:

    class Cow
      heads: 1
    
    class CowCow extends Cow
      m: ->
        alert @heads
        @heads = 11
    
    (new CowCow).m()
    alert (new Cow).heads
    

    alerts 1 twice.

    The third one:

    class Cow
      feet = 9
      m: -> alert feet
    

    is another sort of class variable but this one is very private: feet is not inherited, not visible to subclasses, and not visible to the outside world. The JavaScript version is this:

    var Cow = (function() {
      var feet = 9;
      function Cow() {}
      Cow.prototype.m = function() { return alert(feet) };
      return Cow;
    })();
    

    so you can see that:

    1. feet will be visible to all Cow methods.
    2. All Cow instances will share the same feet.
    3. feet is not public in that you can’t get at it without calling a Cow method (class or instance, either will do).
    4. feet is not visible to subclasses since it isn’t a property of the class and it isn’t in the prototype (and thus it isn’t inherited by instances of subclasses).

    Summary: @utters is sort of a traditional class variable, heads is a public instance variable with a default value, and feet is sort of a private class variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.