Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/stevenmhunt/interfacejs
- Owner: stevenmhunt
- Created: 2013-03-19T01:38:47.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2013-03-19T02:35:26.000Z (almost 12 years ago)
- Last Synced: 2023-03-29T04:16:44.018Z (over 1 year ago)
- Language: JavaScript
- Size: 109 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.