Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

scope - Variable types in CoffeeScript

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...