Skip to content

Instantly share code, notes, and snippets.

@frostney
Last active December 16, 2015 21:59
Show Gist options
  • Save frostney/5503934 to your computer and use it in GitHub Desktop.
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)
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
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