CoffeeScript 中的变量类型 [英] Variable types in CoffeeScript

查看:22
本文介绍了CoffeeScript 中的变量类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太确定 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 

根据我的调查,heads 似乎是公开的,而 feet 是私人的.在弄清楚 nameutters 时,我感到困惑.对于 name 它或多或少编译为 this.name = name 而对于 utters 它编译为 Cow.utters = 1.

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.

所以我的问题是.utters 的范围是什么,应该如何访问?name 的作用域是什么,应该如何访问?

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?

推荐答案

让我们一一看看.

对于第一个:

class Cow
  @utters = 1

this 是当你点击 @utters = 1 时的类本身,所以这个 @utters 是一个类变量.JavaScript 版本是这样的:

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 开始为 1,但在 (new CowCow).m()Cow.utters 之后将是 11一直到 1 点.

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.

第二个:

class Cow
  heads: 1

本质上是一个默认的实例变量;JavaScript 版本如下所示:

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

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

Cow.prototype.heads = 1; 部分意味着 heads 被继承并附加到实例而不是类.

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

结果是这样的:

class Cow
  heads: 1

class CowCow extends Cow
  m: ->
    alert @heads
    @heads = 11

(new CowCow).m()
alert (new Cow).heads

提醒 1 两次.

第三个:

class Cow
  feet = 9
  m: -> alert feet

是另一种类变量,但这是非常私有的:feet 不是继承的,子类不可见,外部世界也不可见.JavaScript 版本是这样的:

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;
})();

所以你可以看到:

  1. feet 将对所有 Cow 方法可见.
  2. 所有 Cow 实例将共享相同的 feet.
  3. feet 不是公开的,因为如果不调用 Cow 方法(类或实例都可以),您就无法获得它.
  4. feet 对子类不可见,因为它不是类的属性,也不在 prototype 中(因此它不会被继承子类的实例).
  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).

<小时>

总结:@utters 是一个传统的类变量,heads 是一个有默认值的公共实例变量,feet 是一种私有类变量.


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.

这篇关于CoffeeScript 中的变量类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆