Look at Figure 02. The function CONSTRUCT takes an argument indicating the structure name and returns the construction function which is an equivilant to constructors in OOP languages. Please note the following
To understand how to control construction order, you need to know the steps of construction. Look at the figure below. Given a structure 'D', which inherits class 'C', which itself inherits structures 'A' and 'B', as an example, construction of an instance of D follows the following steps:
Consider the following code and output:
crx_registerStructure("StructureA",
{
"VERBOSE": 1,
"public CONSTRUCT": function(pA)
{
console.log("CONSTRUCTING StructureA using pA = " + pA);
}
});
crx_registerStructure("StructureB",
{
"VERBOSE": 1,
"public CONSTRUCT": function(pA)
{
console.log("CONSTRUCTING StructureB using pA = " + pA);
}
});
crx_registerStructure("StructureC",
{
"VERBOSE": 1,
"inherits": ["StructureA", "StructureB"],
"public CONSTRUCT": function(pA)
{
console.log("CONSTRUCTING StructureC using pA = " + pA);
}
});
crx_registerStructure("StructureD",
{
"VERBOSE": 1,
"inherits": ["StructureC"],
"public CONSTRUCT": function(pA)
{
console.log("CONSTRUCTING StructureD using pA = " + pA);
}
});
crx_new("StructureD", 5);
Let us follow what happened:
If we wanted the constructor of StructureB execute its useful code before that of the constructor of StructureC, we call it as the first line of code in the constructor of StructureC. Changing the code of StructureC to the following:
crx_registerStructure("StructureA",
{
.
.
.
crx_registerStructure("StructureC",
{
"VERBOSE": 1,
"inherits": ["StructureA", "StructureB"],
"public CONSTRUCT": function(pA)
{
this.CONSTRUCT("StructureB")(pA);
console.log("CONSTRUCTING StructureC using pA = " + pA);
}
});
crx_new("ClassC", 5);
Let us follow what happened:
One very important thing to notice is that the ancestors of StructureC at the StructureB branch fully finished executing their constructors before StructureC finished its own. This is important because it is sufficient in practice. If you are the developer of StructureC, and you want the second ancestor's constructor to be called first, all you care about is the second ancestor of StructureC, StructureB, and its ancestors to finish doing what they need in their constructors before your class begins executing its constructing code. The order of the construction of your ancestors at the StructureB branch would not matter. If it did, it would have been the worry of the developers of StructureB, and its ancesotrs if it had any.
If we want the parameter to be passed up to the constructor of StructureB, we would have to include an explicit constructor call in the constructor of StructureD.
crx_registerStructure("StructureA",
{
.
.
.
crx_registerStructure("StructureD",
{
"VERBOSE": 1,
"inherits": ["StructureC"],
"public CONSTRUCT": function(pA)
{
this.CONSTRUCT("StructureC")(pA);
console.log("CONSTRUCTING StructureD using pA = " + pA);
}
});
crx_new("ClassC", 5);