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

查看:93
本文介绍了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 

根据我的调查,似乎是公开的, / code>是私有的。当找出 name utters 时,我的困惑就出现了。对于 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

这个你命中 @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 将一直保持为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; part意味着 heads 被继承并附加到实例而不是类。

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

结果是: / p>

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

两次提醒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. 所有 Cow 实例将共享相同的

  2. code> feet 不是公共的,因为你不能调用 Cow 方法(类或实例, do)。

  3. 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 是一种传统的类变量,是一个具有默认值的公共实例变量, 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天全站免登陆