Created
October 26, 2011 21:18
-
-
Save rowanmanning/1317895 to your computer and use it in GitHub Desktop.
Just learning a bit of CoffeeScript
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
# represents a person | |
class Person | |
# properties | |
name: null | |
alive: true | |
stomach: 0 | |
bladder: 0 | |
# class constructor | |
constructor: (@name, @alive = true) -> | |
throw "Name is required" if !@name | |
# eat something | |
eat: (quantity) -> | |
@stomach += quantity | |
@die 'burst stomach' if @stomach >= 100 | |
# drink something | |
drink: (quantity) -> | |
@bladder += quantity | |
@die 'ruptured bladder' if @bladder >= 100 | |
# die | |
die: (cause) -> | |
if @alive | |
@alive = false | |
console.log "#{ @name } died of #{ cause }" | |
# represents rowan | |
class Rowan extends Person | |
# properties | |
happiness: 0 | |
# class constructor | |
constructor: (@alive = true) -> | |
super 'Rowan', @alive | |
# eat something | |
eat: (quantity, type = 'food') -> | |
switch type | |
when 'cheese' then @happiness += quantity * 10 | |
when 'beetroot' then @happiness += quantity * 5 | |
super quantity | |
# drink something | |
drink: (quantity, type = 'drink') -> | |
switch type | |
when 'beer' then @happiness += quantity * 10 | |
when 'tea' then @happiness += quantity * 5 | |
super quantity | |
# usage example | |
rowan = window.rowan = new Rowan | |
rowan.eat 5, 'cheese' | |
rowan.eat 42, 'olives' | |
rowan.drink 50, 'tea' | |
rowan.drink 65, 'beer' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment