Interfaces

Though not found in Ruby, I’ve decided to include Interface support in jsclass. Interfaces are found in Java and can be very useful in JavaScript when used judiciously. The idea of an interface is that you create a set of method names with no implementations. You can then insist that objects/classes implement the named methods; if you require an object to have a certain set of methods, you can then throw an exception if it does not.

// In the browser
JS.require('JS.Interface', function(Interface) { ... });

// In CommonJS
var Interface = require('jsclass/src/core').Interface;

To create an interface, just pass in an array of method names:

var IntComparable = new Interface([
    'compareTo', 'lt', 'lte', 'gt', 'gte', 'eq'
]);

var IntStateMachine = new Interface([
    'getInitialState', 'changeState'
]);

You can then test any object to find out whether it implements the required interfaces:

Interface.ensure(someObject, IntComparable, IntStateMachine);

Interface.ensure() tests its first argument against all the supplied interfaces. If it fails one of the tests, an error is thrown that tells you the name of the first method found to be missing from the object.