Last active
August 29, 2015 14:09
-
-
Save rev22/418ad1c36e745e8fd7ff to your computer and use it in GitHub Desktop.
simple javascript/coffeescript inheritance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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