include and extend

The include and extend directives (see Mixins) are available as static methods on all JS.Class classes. Calling include() adds instance methods to the class, and extend adds static methods, just like in Ruby. We could rewrite our TodoItem class as:

        var TodoItem = new JS.Class({
          initialize: function(position) {
            this.position = position;
          },
        
          compareTo: function(other) {
            if (this.position < other.position)
              return -1;
            else if (this.position > other.position)
              return 1;
            else
              return 0;
          }
        });
        
        TodoItem.include(Comparable);

include-ing/extend-ing a class like this will overwrite any existing methods with the same names as those in the mixin, unless you pass false as the second argument.

        TodoItem.include(Comparable, false);

If you include() an object which itself has include or extend properties, these will be treated as directives and applied to your class. So you can do this:

        var Enumerable = { /* methods... */ };
        
        var Comparable = {
          include: Enumerable,
        
          /* methods... */
        };
        
        TodoItem.include(Comparable);
        
        // TodoItem has instance methods from Enumerable and Comparable

include-ing/extend-ing any class will cause its subclasses to inherit the new methods.