Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davo/5bae0e49affc117765c796f9c6b8eace to your computer and use it in GitHub Desktop.
Save davo/5bae0e49affc117765c796f9c6b8eace to your computer and use it in GitHub Desktop.
CoffeeScript Style Guide (using no prototype)
### 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