Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Created September 30, 2012 03:03
Show Gist options
  • Save ryanflorence/3805727 to your computer and use it in GitHub Desktop.
Save ryanflorence/3805727 to your computer and use it in GitHub Desktop.
Sub-classing Array with CoffeeScript
class Collection extends Array
constructor: (items...) ->
@splice 0, 0, items...
sum: -> @reduce (sum, n) -> sum + n
c = new Collection 1, 2, 3
console.assert c[0], 1
c.reverse()
console.assert c[0], 3
sum = c.sum()
console.assert sum, 6
@ryankshaw
Copy link

class Collection extends Array

constructor: (items...) ->
@splice 0, 0, items...

sum: -> @reduce (sum, n) -> sum + n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment