Last active
December 17, 2015 22:09
-
-
Save afeld/5680195 to your computer and use it in GitHub Desktop.
CoffeeScript mixins
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
# a.k.a. _.extend() | |
extend = (obj, mixin) -> | |
for name, method of mixin | |
obj[name] = method | |
include = (klass, mixin) -> | |
extend klass.prototype, mixin | |
MyMixin = | |
foo: -> 'bar' | |
## external ## | |
class User | |
include User, MyMixin | |
u = new User() | |
alert u.foo() | |
## internal ## | |
class Base | |
@include: (mixin) -> | |
include @, mixin | |
class Post extends Base | |
Post.include MyMixin | |
p = new Post() | |
alert p.foo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment