Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stevenmhunt/interfacejs

Provides a simple mechanism for code contracts I.E. interfaces in JavaScript.
https://github.com/stevenmhunt/interfacejs

Last synced: about 1 month ago
JSON representation

Provides a simple mechanism for code contracts I.E. interfaces in JavaScript.

Awesome Lists containing this project

README

        

interfaceJS
===========

Provides a simple mechanism for code contracts I.E. interfaces in JavaScript.

How to use
----------

*Step 1 - Create your interface definition.*

You will use the create() method of interfaceJS to create the interface definition. Provide a list of required members for the interface, and when you check this against your objects it will be able to enforce the interface.

```javascript

var iMyInterface = interfaceJS.create(["prop1", "func1", "etc."])
````

*Step 2 - Validate your objects.*

You will use the implementedBy() method to check if your object implements the interface.

```javascript

var myObj = {
"prop1": 1,
"prop2": 2,
"prop3": 3
};

alert("Does the object implement the interface? " + (iMyInterface.implementedBy(myObj) ? "Yes" : "No");
```

In the case, the implementedBy() method would return false, since the object does not contain all of the required elements defined in the interface.

*More Advanced Features*

Much like languages that implement interfaces natively, you can require than an interface implement any number of other interfaces. You can specify the other interfaces when defining the interface using the implement() method as follows:

```javascript

var iMyChildInterface = interfaceJS.create([
"foo", "bar"
]).implement([
iMyInterface
]);
```

Note that I have taken special care to avoid any future reserved words in the JavaScript language such as "interface", "implements", and "extends". While these keywords are not current in use by the language, they are best avoided.