I was hacking around with CoffeeScript and the need to share part of some functionality between unrelated classes came up.

class User
  move: (somewhere) ->
    somewhere.add(this)

class Post
  move: (somewhere) ->
    somewhere.add(this)

These two classes are completely unrelated except for the fact that they both are movable. So here’s a simple way to introduce a mixin keyword of sorts into CoffeeScript class scope.

Function::mixin = (functions) ->
  for name, value of functions
    @::[name] = value

Movable =
  move: (somewhere) ->
    somewhere.add(this)

class User
  @mixin Movable

class Post
  @mixin Movable

Movable can contain both functions and objects, it will work the same way as if they were defined directly in the class. This is a very straightforward and simple implementation, it provides no inheritance mechanism to mixins like Ruby does, but for most cases this is enough.