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

5.3 Definition and Registration

Before structure instances can be made, they must be defined using either of the two syntaxes mentioned in the introduction. Definitions must not be created or altered using calles to defineProperty(), seal() or other similar methods.

During definition write up, you will encounter syntax errors, as with every other code you write in javascript. After fixing the syntax, you will likely encounter Definition Errors, which are CrxOop's equivalent of javascript syntax errors. These must also be fixed before anything happens. Please refer to the section on errors for more information.

After a definition, a structure can either be resgistered explicitly using crx_registerStructure() which would allow you to give it a name, or registered implicitly during calls to other parts of the library, such as crx_new().

Explicit registration is the recommended way of registration, and you can either do it by assigning a definition to a variable and then calling crx_registerStructure(). In this case instances can be created using the variable name of the definition, or the registered class name.

var structureDefinition =
{
   //DEFINITION
};
crx_registerStructure("myNameSpace.myStructure", structureDefinition);
var instance1 = crx_new(structureDefinition);
//OR
var instance2 = crx_new("myNameSpace.myStructure");

Or by passing the definition immediately to crx_registerStructure(), which is our prefered approach:

crx_registerStructure("myNameSpace.myStructure",
{
   //DEFINITION
});
var instance2 = crx_new("myNameSpace.myStructure");

Note that there is no actual support for name spaces. The full string "myNameSpace.myStructure" is the name of the structure, and not just "myStructure". However the use of ".", or something similar, is useful to avoid name collisions. Also note that structure names can collide with class names and interface names and vice versa. Explicit registration is very useful when it comes to definition errors.

The following is an example of implicit registration:

var structureDefinition =
{
   //DEFINITION
};
var instance1 = crx_new(structureDefinition);

Note that structures registered implicitly can not be re registered explicitly later on. Also note that structures that are not registered implicitly or explicitly do not exist as far as CrxOop is concerned until they are registered. This is important to keep in mind when encountering errors about missing definitions.

With implicit registration, one can define anonymous structures

var instance1 = crx_new(
{
   //DEFINITION
});

The above can be convenient, however beware. Every such call initiates an internal structures registration, which coupled with the immediate need to build an instance, leads to parsing which means resource consumption. If you need to make more than one instance of a structures, do not declare it anonymously. This does not simply mean do not put the call in a loop. The call could also be in a function called multiple times for example. However, remember that you could always make multiple instances of your anonymous structure using the array form of crx_new, instead of a loop, and because the call to crx_new is only made once this way, you suffer no extra resource loss.

However, unlike with classes, the need for anonymous structure, if used for data structures, should be far less common.

5.3.1 crx_registerStructure() / crxOop.crx_registerStructure()

crx_registerStructure(structureName, structureDefinition)
structureName
String
The structure name (string). It is recommended to use '.' to name space your structure names, but remember CrxOop has no actual support for name spaces. If you call your structure "myNameSpace.myStructure", then the structure is called "myNameSpace.myStructure", and not "myStructure".
structureDefinition
Structure Definition
The structure definition (object)
Example
crx_registerStructure("myNameSpace.myStructure",
{
   //DEFINITION
});