-
-
Save iamblue/04acaee263ad56475834 to your computer and use it in GitHub Desktop.
建構式的原型擴充
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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