Skip to content

Instantly share code, notes, and snippets.

@iamblue
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save iamblue/04acaee263ad56475834 to your computer and use it in GitHub Desktop.

Select an option

Save iamblue/04acaee263ad56475834 to your computer and use it in GitHub Desktop.
建構式的原型擴充
# extent 擴充(繼承)
# 在js之中 沒有class概念,但是是拿建構式來實現class
class Animal
(@name, kind) ->
@kind = kind
action: (what) -> "*#{@name} (a #{@kind}) #{what}*"
class Cat extends Animal
(@name) -> super @name, 'cat'
purr: -> @action 'purrs'
kitten = new Cat 'Mei'
kitten.purr! # => "*Mei (a cat) purrs*"
# implements 接口
Huggable =
hug: -> @action 'is hugged'
class SnugglyCat extends Cat implements Huggable
kitten = new SnugglyCat 'Purr'
kitten.hug! # => "*Mei (a cat) is hugged*"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment