JavaScript - Object - Interface

This helper provides interfaces to objects. In traditional OO programing the compiler will fail to compile if a class does not provide the methods defined in the implemented interface. JavaScript does not have a compiler so the use of an interface will stop execution of the script on runtime if a class fails to implement the interface. When failing, an exception to the system log (in a browser; the error console) will be thrown.

Usage I:

This example will check if the interface is implemented when an object is instantiated. This is done by providing this as the first argument to the interface.implements(); method.


    // Defines the interface
    // Name of the interface is "foo" and has the methods "getFoo" and "setFoo"
    var foo = new interface.Interface('foo',['getFoo','setFoo']);

    // A basic class which implements the "foo" interface
    var A = function(){
        // Ensures the class implements the interface
        // If failing to do so, all code after this line will not be executed
        interface.implements(this,foo);
    };
    A.prototype.getFoo = function(){/* Do something... */};
    A.prototype.setFoo = function(v){/* Do something... */};

    // Instantiat the object
    // If interface is not implemented, the script will terminate upon instantiation 
    var a = new A();

Usage II:

This example will check if the interface is implemented after the object is instantiated. This is done by providing the instantiated object as the first argmuent to the interface.implements(); method.


    // Defines the interface
    // Name of the interface is "foo" and has the methods "getFoo" and "setFoo"
    var foo = new interface.Interface('foo',['getFoo','setFoo']);

    // A basic class which implements the "foo" interface
    var A = function(){/* Do something... */};
    A.prototype.getFoo = function(){/* Do something... */};
    A.prototype.setFoo = function(v){/* Do something... */};

    // Instantiat the object
    var a = new A();
    
    // Ensures the class implements the interface
    // If failing to do so, all code after this line will not be executed
    interface.implements(a,foo);

Usage III:

This example implements two interfaces.


    // Defines the interfaces
    var foo = new interface.Interface('foo',['getFoo','setFoo']);
    var bar = new interface.Interface('bar',['alterFoo']);

    // A basic class which implements "foo" and "bar"
    var A = function(){
        // Ensures the class implements the interfaces
        // If failing to do so, all code after this line will not be executed
        interface.implements(this,foo,bar);
    };
    A.prototype.getFoo = function(){/* Do something... */};
    A.prototype.setFoo = function(v){/* Do something... */};
    A.prototype.alterFoo = function(v){/* Do something... */};

    // Instantiat the object
    var a = new A();

Example:

Each of the two links below will instantiat an object which implements an interface. One object fails to implement the interface and will not be created due to this.

Try to instantiat test object one: Status of test object number one unknown!

Try to instantiat test object two: Status of test object number two unknown!

Code for this example here.

Note:

A try{}catch(){} arround the interface.implements() method will mask the error message from the error log! It will also eliminate the core function of an interface since the try{}catch(){} will let the script continue to execute.

Acknowledgment:

This page is a result of techniques described by Ross Harmes and Dustin Diaz

Page by: Trygve Lie