Implementing a Simple "MoneyAccount" in CoffeeScript Please critique this CoffeeScript code. Is it idiomatic?

MoneyAccount = (balance = 0) -&gt;
  obj = {
    withdraw: (x) -&gt;
      if balance &lt; x
        throw new Error("not enough funds in account")
      else
        balance -= x
        console.log "withdrew funds."
        x
    deposit: (x) -&gt;
      if (typeof x isnt 'number' || x &lt; 0)
        throw new Error("bad deposit amount " + x)
      else
        balance += x
    printBalance: -&gt;
      console.log balance
  }
  obj
This seems closely tied to your other question. I posted an answer for that one, which I'll incorporate/reference here.

Calling it MoneyAccount (in "UpperCamelCase") implies that it's a constructor. But it's not quite; it's just a function. But I assume that's done on purpose in order to have "private" variables (i.e. the closed-over balance argument) while still allowing one to invoke it with or without the new keyword.

Still, there are a couple of things you could do:


You're diligent in checking the input for deposit and withdraw... but not the initial balance. So account = MoneyAccount("foo") would spell trouble right away.
But even if you check the type of the initial balance, it'd still allow someone input a negative initial balance. But the way withdraw works seems to imply that a negative balance is impossible.
Incidentally, I could also say account = MoneyAccount(1/0) and have Infinity money! Sounds nice, but perhaps not what you want.
Similar to the above, there's nothing stopping you from withdrawing or depositing a negative or infinite amount.
As mentioned in the answer I linked to, you don't need if...else branches if you throw an an exception; the function will exit if it throws.
There's no way to just get the balance, which would seem problematic for an account. You can print it, sure, but you can't just get it. If you add a method to do that, you can skip the console.log stuff (which incidentally, you're only doing for withdraw but not for deposit). Logging stuff isn't really the responsibility of an account object anyway; leave that to external code.
x should probably be named amount instead, just to be descriptive.
You might want to throw a RangeError in withdraw to be a bit more precise.
Return values are a little haphazard. deposit returns the new balance while withdraw returns the amount withdrawn.
The obj var is unnecessary; you can just leave it out entirely. And you can even leave out the curly brackets (which I personally like to do in a case like this, but it's a matter to taste, really).


In the end, I get this:

MoneyAccount = (balance = 0) -&gt;
  # Adapted from the linked answer
  assertValidAmount = (amount) -&gt;
    number = Number amount
    if isNaN(number)
      throw new TypeError("Expected a numeric amount")
    if not isFinite(number) or number &lt; 0
      throw new RangeError("Expected a positive, finite amount")

  assertValidAmount balance

  # everything below will be interpreted as an object
  # and returned, even without the curly braces

  balance: -&gt; balance

  withdraw: (amount) -&gt;
    assertValidAmount amount
    throw new RangeError("Insufficient funds") if amount &gt; balance
    balance -= amount

  deposit: (amount) -&gt;
    assertValidAmount amount
    balance += amount


That should be pretty safe.