Programming Structures and Syntax
last updated on: 03/23/17
| tags: OOP, structures, syntax
### Inheritance
A certain type might extend on a more generic one, by using the **Inherit** function like:
```javascript
SomeClass.Inherit(ParentClass, "SomeClass");
```
The second argument in the call, is used to register the constructor of the new type in the global **Function.classes** array.
### Implementing interfaces
Because of the JavaScript specifics, nothing forbids interfaces from having some default functionality implemented. The logic written in the interface definition, will get transfered over to the classes that implement that interface. This can be found usefull in many cases.
To implement a specific interface, one must use the **Implement** function:
```javascript
SomeClass.Implement(ISomeInterface);
```
In this case, the prototype of SomeClass would receive the properties and methods, defined by the interface, along with some default implementation. This feature, based on the specifics of the javascript language, can be found useful in certain situations. The Implement method can also be used with another type of structure called an **Implementer**. Implementers can be usefull in cases, where the implementation of a certain interface is not trivial, and requires conditional branching. An implementer is declared as follows:
```javascript
function ISomeInterfaceImpl() { }
ISomeInterfaceImpl.Interface(ISomeInterface);
ISomeInterfaceImpl.classInitialize = function(cls) {
cls.prototype.someMethod = function(...){...};
cls.prototype.anotherMethod = function(...){...};
});
};
```
The __classInitialize__ function will be called, when the type "cls" is constructed.
Implementers can also specify, which types they support. This is done using the __RequiredTypes__ function:
```javascript
ISomeInterfaceImpl.RequiredTypes("ClassA, ClassB, ...");
```
### Providing services
Services can be created using the following statement:
```javascript
SomeClass.prototype.provideAsServices= new InitializeArray("Some class provides ISomeService as a service.", ["ISomeService"]);
```
Services are useful in situations when an object needs to communicate with any parent object up the logical hierarchy.