Classes.js

The Classes.js file provides the public Class.New() method, which has the following signature.

New(classname: String, superclass: Class)

The Class.New() method creates a new JavaScript class. After the class has been created, it has a number of methods and properties that can be invoked.

As a general rule, call the setProperties() method after calling the Class.New() method, to make the class a class with properties, or the isArrayOf() method, to make an array class. After invoking one of these methods, you can create instances of the class by using the new keyword.

Classes with properties have set and get methods for each property. In the following example, a class is created with a color property. If the corresponding color() method is called with a parameter, the method sets the value of the color property. If it is called without a parameter, the method gets the value of the color property. The method checks the types of values used.

The following example shows the use of the Class.New() method to create a class with properties.

var Animal = Jade.Class.New("Animal");             // define the class
Animal.setProperties({color:String, age:Number});  // give it properties

var myAnimal = new Animal({color:"blue",age:14});  // instantiate it
myAnimal.age();               // get a property - returns 14
myAnimal.color("red");        // change a property
myAnimal.color();             // now returns "red"
myAnimal.age(Animal);         // throws exception - fails type-check

var Lion = Jade.Class.New("Lion", Animal); // subclass Animal
Lion.setProperties({kills:Number});        // add subclassed properties

var myLion = new Lion({kills:12,color:"yellow"});  // instantiate it
                              // can set properties defined in superclass
myLion.kills(45);             // set the kills
myLion.color();               // returns "yellow"
myLion.Classname();           // returns "Lion"
myLion instanceof Lion;       // returns true
myLion instanceof Animal;     // returns true
                              // a subclass is an instance of its superclass
myLion.Superclass();          // returns Animal
myLion.Properties();     // returns {kills:Number, color:String, age:Number}

If the isArrayOf() method is called instead of the setProperties() method, the class is an array class. After invoking this method, you can create instances of the array by using the new keyword.

The following example shows the use of the Class.New() method to create an array.

var AnimalArray = Jade.Class.New("AnimalArray");
AnimalArray.isArrayOf(Animal);                  // make it into an array

var myAA = new AnimalArray([myAnimal, myLion]); // instantiate it
                                                // Can set initial contents
myAA.Array();                                   // returns [myAnimal,myLion]
myAA.Array([]);                                 // sets the array to empty
myAA.Add(myAnimal);                             // adds to the array

var LionArray = Jade.Class.New("LionArray", AnimalArray);
                                                // subclasses AnimalArray
LionArray.isArrayOf(Lion);                      // make it into an array

var myLA = new LionArray([myLion]);             // instantiate it
myLA.Add(myAnimal);                           // throws type-check exception

The Class.New() method creates a Class object with the following properties and methods.

Method or Property Description
ClassName(): String Returns the name of the class.
IsArray(): Boolean Returns whether the class is an array class (by default they are not, unless isArrayOf(memberType) has been called.
SuperClass(): Class Returns a reference to the superclass.
isArrayOf(memberType: Class) Makes the class into an array class (can be called once only), with members of the specified type. Methods are added to conform to the TypedArrayClass interface.
setProperties(properties: Object) For example, setProperties({name:String, age:Number, dob:Date, children:PersonArray}); gives the class new properties, and can be called once only. The names and types of the class properties are those of the properties of the parameter. Methods are added to the class to conform to the TypedPropertyClass interface.

Instances of the Class Class (that is, the actual objects created when you use the new keyword to create an instance of the class) have the following properties.

Property Description
ClassName(): String Returns the name of the class.
IsArray(): Boolean Returns whether the class is an array class (by default, classes are not array classes, unless isArrayOf(memberType) has been called).
SuperClass(): Class Returns a reference to the superclass.

The TypedArrayClass interface provides the following additional functions.

Function Description
MemberType(): Class Returns the member type of the array.
<constructor>(valueArray: Array of MemberType) Optionally invokes the Array(valueArray) function in the TypedArrayClass interface, if a parameter is provided.

Instances of the TypedArrayClass interface have the following additional functions.

Function Description
MemberType(): Class Returns the member type of the array.
Array(valueArray: Array of MemberType) Replaces the array with the values passed, after type-checking.
Array(): Array Returns a copy of the private internal type-checked array object.
Add(value: MemberType) Adds a value to the array. If the value is not of MemberType, an exception is raised.

The TypedPropertyClass interface provides the following additional functions.

Function Description
Properties(): Object Returns the properties passed to the setProperties() function when the class was defined.
<constructor>(values: Object) Invokes the SetProperties(values) function on the instance, if the parameter is provided.

Instances of the TypedPropertyClass interface have the following additional functions:

Function Description
Properties(): Object Returns the properties passed to setProperties() when the class was defined.
SetProperties(values: Object) Sets the values of any number of properties after type-checking. For example, {name: "Andrew", age: 21, children: myChildrenArray}.

Instances of the TypedPropertyClass interface also have get and set methods with the names specified in the setProperties() method, as shown in the following example if the Person class has name and age properties.

Person.setProperties({name:String, age:Number});

You can set and get the value of the name property of an instance of the Person class, as follows.

emp.name("Bill");                  // sets the value of the name property
emp.name();                        // gets the value of the name property