Skip to content

Instantly share code, notes, and snippets.

@satyr
Created August 29, 2010 11:29
Show Gist options
  • Save satyr/556213 to your computer and use it in GitHub Desktop.
Save satyr/556213 to your computer and use it in GitHub Desktop.
prototypal coffee
# for CoffeeScript 0.9.3
@clone = (it, mixes...) ->
i = new (clone.r it)
i.__proto__ ||= it
for mix in mixes
if typeof mix is 'function'
mix.call i, it, i
else
for k, v of mix then i[k] = v
i
@clone.r = (it) ->
(f = ->).prototype = it
f
@clone.p = (it, that) ->
n = 1
while it = it.__proto__
return n if it is that
++n
0
###
bird = wing: on, fly: -> @wing
wingless = wing: off
duck = clone bird
dodo = clone bird, wingless, extinct: yes
donaldo = clone duck, wingless, domestic: yes
ng = (bad) -> throw Error() if bad
ok = (gud) -> ng not gud
ok duck.fly()
ng donaldo.fly()
ok clone.p dodo, bird
ng clone.p dodo, wingless
ok donaldo instanceof clone.r bird
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment