類繼承和超級
CoffeeScript 提供了一個基本的類結構,允許你在單個可指定表示式中命名你的類,設定超類,分配原型屬性以及定義建構函式。
以下小例子:
class Animal
  constructor: (@name) ->
  move: (meters) ->
    alert @name + " moved #{meters}m."
class Snake extends Animal
  move: ->
    alert "Slithering..."
    super 5
class Horse extends Animal
  move: ->
    alert "Galloping..."
    super 45
sam = new Snake "Sammy the Python"
tom = new Horse "Tommy the Palomino"
sam.move()
tom.move()
這將顯示 4 個彈出視窗:
- 冰蟲…
- Sammy the Python 移動了 500 萬。
- 舞動……
- 帕洛米諾的湯米移動了 45 米。