CoffeeScript 类变量和实例变量

类变量和实例变量

问题

你想创建类变量和实例变量(属性)。

解决方案

类变量

class Zoo
  @MAX_ANIMALS: 50
  MAX_ZOOKEEPERS: 3

  helpfulInfo: =>
    "Zoos may contain a maximum of #{@constructor.MAX_ANIMALS} animals and #{@MAX_ZOOKEEPERS} zoo keepers."

Zoo.MAX_ANIMALS
# => 50

Zoo.MAX_ZOOKEEPERS
# => undefined (it is a prototype member)

Zoo::MAX_ZOOKEEPERS
# => 3

zoo = new Zoo
zoo.MAX_ZOOKEEPERS
# => 3
zoo.helpfulInfo()
# => "Zoos may contain a maximum of 50 animals and 3 zoo keepers."

zoo.MAX_ZOOKEEPERS = "smelly"
zoo.MAX_ANIMALS = "seventeen"
zoo.helpfulInfo()
# => "Zoos may contain a maximum of 50 animals and smelly zoo keepers."

实例变量

你必须在一个类的方法中才能定义实例变量(例如属性),在constructor结构中初始化你的默认值。

class Zoo
  constructor: ->
    @animals = [] # Here the instance variable is defined

  addAnimal: (name) ->
    @animals.push name

zoo = new Zoo()
zoo.addAnimal elephant

otherZoo = new Zoo()
otherZoo.addAnimal lion

zoo.animals
# => [elephant]

otherZoo.animals
# => [lion]

警告!

不要试图在constructor外部添加变量(即使在elsewhere中提到了,由于潜在的JavaScript的原型概念,这不会像预期那样运行正确)。

class BadZoo
  animals: []           # Translates to BadZoo.prototype.animals = []; and is thus shared between instances

  addAnimal: (name) ->
    @animals.push name  # Works due to the prototype concept of Javascript

zoo = new BadZoo()
zoo.addAnimal elephant

otherZoo = new BadZoo()
otherZoo.addAnimal lion

zoo.animals
# => [elephant,lion] # Oops...

otherZoo.animals
# => [elephant,lion] # Oops...

BadZoo::animals
# => [elephant,lion] # The value is stored in the prototype

讨论

Coffeescript会将类变量的值保存在类中而不是它定义的原型中。这在定义类中的变量时是十分有用的,因为这不会被实体属性变量重写。

作者:andy,如若转载,请注明出处:https://www.web176.com/coffeescript/10695.html

(0)
打赏 支付宝 支付宝 微信 微信
andy的头像andy
上一篇 2023年2月26日
下一篇 2023年2月26日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注