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

3.1 Instantiation

The equivalent of the C++ "new" keyword is the function crx_new() / crxOop.crx_new(). The function is the only way to create class instances. The function provides four overrides which allow the creation of single or multiple instances. All four overrides allow the use of anonymous class definitions, a feature which can be abused very easily.

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

var gMyClass = crx_new("MyClass", 1, 1);
(1,1)


crx_new(length, classDefinitionOrClassName, [parameter01, parameter02, ...])
length
integer
The number of instances to create
classDefinitionOrClassName
Class Definition or String
The class Definition (object) or name (string)
parameter01, parameter02, ...
Mixed
The parameters to be passed to the class constructor. The parameters are passed for each construction. Optional.
Return
Array
An array of class instances
Example
crx_registerClass("MyClass",
{
   "VERBOSE": 1,
   "public CONSTRUCT": function(pA, pB)
   {
      console.log("(" + pA + "," + pB + ")");
   }
});

var gMyClass = crx_new(2, "MyClass", 1, 1);
(1,1)
(1,1)


crx_new(length, parametersArray, classDefinitionOrClassName)
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.
length
integer
The number of instances to create
classDefinitionOrClassName
Class Definition or String
The class Definition (object) or name (string)
Return
Array
Returns an array of class instances
Example
crx_registerClass("MyClass",
{
   "VERBOSE": 1,
   "public CONSTRUCT": function(pA, pB)
   {
      console.log("(" + pA + "," + pB + ")");
   }
});

var gMyClass = crx_new(4, [[1,1], [2,2]], "MyClass");
(1,1)
(2,2)
(2,2)
(2,2)


crx_new(length, parametersFunction, classDefinitionOrClassName)
length
integer
The number of instances to create
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.
classDefinitionOrClassName
Class Definition or String
The class Definition (object) or name (string)
Return
Array
Returns an array of class instances
Example
crx_registerClass("MyClass",
{
   "VERBOSE": 1,
   "public CONSTRUCT": function(pA, pB)
   {
      console.log("(" + pA + "," + pB + ")");
   }
});

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