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.2 Instantiation

The equivalent of the "new" keyword that is found in many programing languages is the function crx_new() / crxOop.crx_new(). The function works the same way as it does for classes, and has the same exact signature. It is the only way to create structure instances, and provides four overrides which allow the creation of single or multiple instances. All four overrides allow the use of anonymous structure definitions, a feature which can be abused very easily.

crx_new(structureDefinitionOrStructureName, [parameter01, parameter02, ...])
structureDefinitionOrStructureName
Class Definition or String
The structure Definition (object) or name (string)
[parameter01, parameter02, ...]
Mixed
The parameters to be passed to the structure constructor.
Return
Object
A structure instance
Example
crx_registerStructure("MyStructure",
{
   "VERBOSE": 1,
   "public CONSTRUCT": function(pA, pB)
   {
      console.log("(" + pA + "," + pB + ")");
   }
});

var gMyStructure = crx_new("MyStructure", 1, 1);
(1,1)


crx_new(length, structureDefinitionOrStructureName, [parameter01, parameter02, ...])
length
integer
The number of instances to create
parameter01, parameter02, ...
Mixed
The parameters to be passed to the structure constructor. The parameters are passed for each construction. Optional.
Return
Array
An array of structure instances
Example
crx_registerStructure("MyStructure",
{
   "VERBOSE": 1,
   "public CONSTRUCT": function(pA, pB)
   {
      console.log("(" + pA + "," + pB + ")");
   }
});

var gMyStructure = crx_new(2, "MyStructure", 1, 1);
(1,1)
(1,1)


crx_new(length, parametersArray, structureDefinitionOrStructureName)
parametersArray
Array of Arrays
An an array of arrays such as parametersArray[i] is an array of parameters to be passed to the constructor of instance i; If parametersArray is shorter than the number of instances to create, the last element is used for the construction of the remaining instances.
Return
Array
An array of structure instances
Example
crx_registerStructure("MyStructure",
{
   "VERBOSE": 1,
   "public CONSTRUCT": function(pA, pB)
   {
      console.log("(" + pA + "," + pB + ")");
   }
});

var gMyStructure = crx_new(4, [[1,1], [2,2]], "MyStructure");
(1,1)
(2,2)
(2,2)
(2,2)


crx_new(length, parametersFunction, structureDefinitionOrStructureName)
parametersFunction
function
A function that takes a single integer, which is the current index of the instance created, and returns an array of parameters to be passed to the constructor.
Return
Array
Returns an array of structure instances
Example
crx_registerStructure("MyStructure",
{
   "VERBOSE": 1,
   "public CONSTRUCT": function(pA, pB)
   {
      console.log("(" + pA + "," + pB + ")");
   }
});

var gMyStructure = crx_new(4, function(pIndex)
{
   return [pIndex, pIndex * pIndex];
}, "MyStructure");
(0,0)
(1,1)
(2,4)
(3,9)