Skip to content

Instantly share code, notes, and snippets.

@rev22
Last active August 29, 2015 14:09
Show Gist options
  • Save rev22/418ad1c36e745e8fd7ff to your computer and use it in GitHub Desktop.
Save rev22/418ad1c36e745e8fd7ff to your computer and use it in GitHub Desktop.
simple javascript/coffeescript inheritance
Person = ->
return
Person::hello = -> alert "hello"
John = ->
Person.call @
return
John::__proto__ = Person:: # This is shorter, but changing __proto__ is deprecated
# John:: = __proto__: Person::, constructor: John # this would be equivalent to the former line, but is longer!
John::hello = -> alert "ciao"
Mark = do(parent = Person)@>
c = -> parent.call @
c::__proto__ = parent::
c
makeClass = (prototype)->
constructor = prototype.constructor
constructor:: = prototype
constructor
Eric = makeClass
__proto__: Person::
constructor: -> Person.call @; return
hello: -> "hej"
(new ((new Eric()).constructor)()).hello()
# Rough, untested new simulation
explicit_new = (constructor)->
# it's actually possible that these two fields are actually set *after* object construction!
x =
__proto__: constructor.prototype
constructor: constructor
constructor.call(@) ? x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment