Skip to content

Instantly share code, notes, and snippets.

@dmitry-vsl
Created November 20, 2013 17:04
Show Gist options
  • Save dmitry-vsl/7566867 to your computer and use it in GitHub Desktop.
Save dmitry-vsl/7566867 to your computer and use it in GitHub Desktop.
include = (mixins...) ->
to: (clazz)->
result = class extends clazz
for mxn in mixins
for own k,v of mxn(clazz)
result::[k] = v
result
# This is mixin. It can access methods of the class it is mixed to
TestMixin = (target) ->
foo: -> target::foo.apply @;console.log 'bar'
# define class with mixin. With requirejs it would be
# define [], ->
# include(TestMixin).to class
# foo: -> console.log 'baz'
Test = include(TestMixin).to class
foo: -> console.log 'baz'
someVar = new Test
someVar.foo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment