Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 2. CoffeeScript Classes > Extending Classes

2.5. Extending Classes

Mixins are pretty neat, but they’re not very object orientated. Instead, let’s integrate mixins into CoffeeScript’s classes. We’re going to define a class called Module that we can inherit from for mixin support. Module will have two static functions, @extend() and @include(), which we can use for extending the class with static and instance properties, respectively:

moduleKeywords = ['extended', 'included']

class Module
  @extend: (obj) ->
    for key, value of obj when key not in moduleKeywords
      @[key] = value

    obj.extended?.apply(@)
    this

  @include: (obj) ->
    for key, value of obj when key not in moduleKeywords
      # Assign properties to the prototype
      @::[key] = value

    obj.included?.apply(@)
    this

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial