-
-
Save davo/5bae0e49affc117765c796f9c6b8eace to your computer and use it in GitHub Desktop.
CoffeeScript Style Guide (using no prototype)
This file contains 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
### flipjs.io CoffeeScript Style Guide ### | |
do -> | |
############################################################ CLASS ### | |
class Animal | |
constructor: (@name) -> | |
initProps = => | |
@legs = 4 | |
@canEat = canEat | |
@canWalk = canWalk | |
@canGrowl = canGrowl | |
return | |
show = (behavior) => console.log "#{@name} #{behavior}" | |
canEat = -> show 'eats...' | |
canWalk = -> show 'walks...' | |
canGrowl= -> show 'growls...' | |
initProps() | |
############################################################ CLASS ### | |
class Dog extends Animal | |
constructor: -> | |
super | |
initProps = => | |
@doTrick = doTrick | |
@learnNewTrick = learnNewTrick | |
@tricks = ['playing dead', 'fetches a ball', 'wags its tail'] | |
show = (behavior) => console.log "#{@name} #{behavior}" | |
doTrick = (trick) -> | |
show if trick in @tricks then trick else '*whines*' | |
learnNewTrick = (trick) -> @tricks.push(trick) | |
initProps() | |
############################################################ MAIN ### | |
main = -> | |
dog = new Dog('Rusty') | |
dog.canEat() | |
dog.canWalk() | |
dog.canGrowl() | |
dog.doTrick('playing dead') | |
dog.doTrick('catches a mouse') # doesnt know how, dog whines | |
dog.doTrick('fetches a ball') | |
dog.learnNewTrick('catches a mouse') | |
dog.doTrick('catches a mouse') # now can do new trick | |
dog.doTrick('wags its tail') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment