O'Reilly logo

CoffeeScript, 2nd Edition by Trevor Burnham

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mini-Project: Checkbook Balancer

Let’s put these ideas together into an old-school computer program. All we want to do is keep track of where we’re keeping our money across three accounts: checking, savings, and our mattress. We can deposit or withdraw money from each of these accounts. Let’s implement these two actions as methods on an object that represents the account, and return that object from a function so that we can create as many accounts as we need without repeating our implementation code:

 createAccount = (name) ->
  {
  name: name
  balance: 0
 
  description: ->
 "​​#{​@name​}​​: ​​#{​dollarsToString(@balance)​}​​"
 
  deposit: (amount) ->
  @balance += amount
 @
 
  withdraw: (amount) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required