Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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