CrxOop CrxOop: Bringing Object Oriented Programming, and Proper Prototype Based Programming, To Javascript
The aim of the library documented below is to provide developers with a solution to allow development using interfaces and classes as they are known in other object oriented programming (OOP) languages such as C++, C#, Java and PHP. Further more, V1.4 introduces structures, a generalization of the concept of prototypal inheritance, known here as POBP.
Subject
Body*
Email
SUBMIT CLOSE
Menu

4.2 Syntax

Interfaces are defined using plain objects where the keys define the names of functions.

crx_registerInterface("myNameSpace.myInterface",
{
   INHERITS: ["myNameSpace.someOtherInterface1", "myNameSpace.someOtherInterface2"],
   "function1": 0,
   "function2": 0
});

The above interface declares two methods to be implemented by implementing classes along with the methods declared by the two interfaces it inherits, "myNameSpace.someOtherInterface1" and "myNameSpace.someOtherInterface2". An interface may inherit one or more other interfaces, using the definition keyword 'INHERITS'. Interfaces may not inherit other interfaces defining functions with the same name. The same applies for classes, classes may not implement multiple interfaces such as one of the interfaces that it implements defines a method already defined in another interface that it implements.

Classes may implement interfaces using the definition keyword 'INHERITS' as shown below.

JS (Tree)
crx_registerInterface("ExampleInterface1",
{
   "function1": 0,
   "function2": 0
});
crx_registerInterface("ExampleInterface2",
{
   INHERITS: ["ExampleInterface1"],
   "function3": 0
});
crx_registerInterface("ExampleInterface3",
{
   "function4": 0
});


crx_registerClass("ExampleClass1",
{
   PUBLIC:
   {
      VIRTUAL:
      {
         FUNCTIONS:
         {
            "function1": function(){}
         }
      }
   }
});
crx_registerClass("ExampleClass2",
{
   IMPLEMENTS: ["ExampleInterface2", "ExampleInterface3"],
   EXTENDS: "ExampleClass1",
   PUBLIC:
   {
      VIRTUAL:
      {
         FUNCTIONS:
         {
            "function2": function(){},
            "function3": function(){},
            "function4": function(){}
         }
      }
   }
});
var instance = crx_new("ExampleClass2");
JS (Verbose)
crx_registerInterface("ExampleInterface1",
{
   "function1": 0,
   "function2": 0
});
crx_registerInterface("ExampleInterface2",
{
   INHERITS: ["ExampleInterface1"],
   "function3": 0
});
crx_registerInterface("ExampleInterface3",
{
   "function4": 0
});


crx_registerClass("ExampleClass1",
{
   "VERBOSE": 1,
   "public virtual function function1": function(){}
});
crx_registerClass("ExampleClass2",
{
   "VERBOSE": 1,
   "implements": ["ExampleInterface2", "ExampleInterface3"],
   "extends": "ExampleClass1",
   "public virtual function function2": function(){},
   "public virtual function function3": function(){},
   "public virtual function function4": function(){}
});
var instance = crx_new("ExampleClass2");

Notice how the class "ExampleClass2" did not need to implement "function1" defined in "ExampleInterface1". This is because "function" is already defined by another class in its extension chain. However, CrxOop will issue a warning.