Modules

Ruby modules are collections of methods that can be mixed into classes. The instance methods in a module cannot be accessed from outside it, they can only be used in classes that include or extend using the module.

JS.Class has some functionality to mimick modules. JS.Module creates objects that have just two methods – an included() and an extended() hook – that can be mixed into classes but cannot have their instance methods modified. This allows you to create secure mixins.

        var Comparable = new JS.Module({
          extend: {
            compare: function(a,b) {
              return a.compareTo(b);
            }
          },
        
          lt: function(object) {
            return this.klass.compare(this, object) == -1;
          },
          lte: function(object) {
            return this.klass.compare(this, object) < 1;
          },
          gt: function(object) {
            return this.klass.compare(this, object) == 1;
          },
          gte: function(object) {
            return this.klass.compare(this, object) > -1;
          },
          eq: function(object) {
            return this.klass.compare(this, object) == 0;
          }
        });

(JS.Class comes with a Comparable module, so you don’t have to write you own.)

Comparable’s instance methods are now not accessible unless the module is mixed into a class:

        TodoItem.include(Comparable);
        // Now TodoItem has all the methods defined above