Last active
December 16, 2015 21:59
-
-
Save frostney/5503934 to your computer and use it in GitHub Desktop.
Mixins in CoffeeScript (does not override existing properties, but makes them into a function or an array)
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
mixin = (target, source...) -> | |
return unless target or source | |
for s in source | |
for key, value of s | |
unless Object.hasOwnProperty.call target, key | |
console.log key | |
console.log value | |
target[key] = value | |
else | |
oldRef = target[key] | |
target[key] = do -> | |
if typeof oldRef is 'function' and typeof value is 'function' | |
return -> | |
oldRef.apply @, arguments | |
value.apply @, arguments | |
else | |
[oldRef, value] | |
null |
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
class MyNewClass | |
mixin @::, {a: 5}, {a: 6} | |
constructor: -> | |
c: -> alert 'c' | |
mnc = new MyNewClass() | |
console.log mnc.a # [5, 6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment