Skip to content

An extension to Backbone making it possible to program with Java-like runtime-enforced interfaces

Notifications You must be signed in to change notification settings

magnhaug/backbone-interfaces

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Backbone-interfaces

An extension to Backbone making it possible to program with Java-like runtime-enforced interfaces

Example usage

Automatic initialization-time interface implementation verification:

var MyInterface     = Backbone.Interface.extend({foo: function(){}});
var GoodClass       = Backbone.Model.extend({foo: function(){console.log("foo");} });
var BadClass        = Backbone.Model.extend({});

GoodClass.implements(MyInterface);
BadClass.implements(MyInterface);

new GoodClass(); // Ok
new BadClass();  // Throws InterfaceNotImplementedException

Runtime manual interface implementation verification:

var MyInterface     = Backbone.Interface.extend({f: function(){}});
var GoodClass       = Backbone.Model.extend({f: function(){console.log("foo");} });
var GoodInstance    = new GoodClass();
var BadClass        = Backbone.Model.extend({});
var BadInstance     = new BadClass();

Backbone.Interface.verify(MyInterface, GoodInstance); // Ok
Backbone.Interface.verify(MyInterface, BadInstance);  // Throws InterfaceNotImplementedException

Why?

The idea of implementing this came to me after writing yet another method with specific requirements for the parameter objects. Interfaces as a concept is traditionally used as a cheap emulation of multiple inheritance. In a dynamic and prototypical language like JavaScript this is not needed. Though what this will give your project, is a cheap emulation of static typing, for certain purposes. A way of enforcing certain requirements.

About

An extension to Backbone making it possible to program with Java-like runtime-enforced interfaces

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published