JavaScript like other object oriented languages, supports the notion of objects that are collections of named variables. These variables are usually called object
properties and are referenced via a dot notation. For example:
var o = new Object();
o.color = "red";
Properties may be any JavaScript primitive type or they may be objects themselves. They may also be functions in which case they are referred to as object
methods. Properties may also be referenced using an array index notation. For example:
o["color"] = "blue";
This feature allows objects to function as associative arrays. See # for more details.
Creating Objects
Objects are created via the
new operator which returns a reference to the object. Objects are not created in the local or global variable store. Rather, they are created in the JavaScript memory heap. When the result of the new operator is assigned to a variable, what is assigned is a reference to the object. There may exist multiple references to an object at any time. When there are no remaining references, the objects is not accessible and is deleted by the EJS garbage collector. For example:
var o = new Object();
var o2 = o;
After the assignment to o2, there are two references to the object. You an explicitly remove a reference to an object by assigning another value or null to a variable. Continuing the example above, if we assign null to the variable "o", then the object created will have only one reference. Assigning null to o2 will remove the final reference and the object will be destroyed.
When objects are passed to functions, they are passed by reference. The object is not copied.
Object Constructors
When the new operator is used, what is supplied to the right of the operator is an object constructor. The
Object() function is actually the constructor for the object class. EJS includes builtin constructors for the Object and Array classes, and you may create your own constructors for your custom objects.
An object constructor is a global function with the name of the desired object class that returns the required object instance. By convention, object constructors start with an upper case letter. For example:
function MyObj()
{
var o = new Object(); // Create a new bare object
o.height = 0; // Create and initialize a height property
o.width = 0; // Create and initialize a width property
return o; // Return the object
}
var firstObj = new MyObj(); // Create a first instance
var secondObj = new MyObj(); // Create a second instance
This creates two instances
firstObj and
secondObj using the MyObj constructor. Constructors create the required properties by simply initializing the properties.
Object Methods
Object methods are created similarly to object properties by assigning a function to a named object property. For example:
//
// Method for MyObj
//
function print()
{
print("height = " + this.height);
print("width = " + this.width);
}
//
// Constructor
//
function MyObj()
{
var o = new Object(); // Create a new bare object
o.height = 0; // Create and initialize a height property
o.width = 0; // Create and initialize a width property
o.print = print;
return o; // Return the object
}
var o = MyObj(); // Create the object
o.print(); // Invoke the print method
The modified MyObj constructor now assigns the print method and creates a print property in the newly minted object.
SubClassing
To subclass an object class, you create a new object constructor that invokes the subclass constructor. You then add your properties to the object returned by the base constructor. For example:
function YourObj()
{
var o = new MyObj();
o.color = "black";
return o;
}
JavaScript does not express classes as C++ or Java does. Rather objects are created by constructors that determine the properties of the object. ECMAScript does have a way to replicate objects more quickly by creating an object prototype. This consists of creating a template object prototype that is used when the constructor is invoked. This paradigm is often unfamiliar to developers and so EJS has chosen not to implement it. The proposed JavaScript 2.0 release may include a class construct which is under consideration for inclusion in a future version of EJS.
Standard Methods
JavaScript objects define the
toString method which converts the object representation into a string. The default toString method returns "[object this]". You may override the toString method by assigning your own function to the toString property in your object constructor.
Arrays
JavaScript arrays build upon the object class and provide a powerful array base type that can be indexed numerically or via strings. In reality, JavaScript Arrays are a subclass of the base Object class with a little extra functionality.
When using string indicies, the array implements an associative array. When using numeric indicies, it behaves more like a classical array. In both cases, arrays will grow on demand as new elements are stored in the array and will be automatically garbage collected when the array is no longer referenced or the program / script exits.
Array elements are read and written using the [] operator. For example:
customers["bill"] = new Customer(); // Using a string index
visited[43] = 1; // Using a numeric index
Associative Arrays
Associative arrays are very useful construct in a scripting language, especially when combined with the
for / in statement. Associative arrays allow you to store arbitrary data indexed by a string key. The data stored can be a primitive type or it may be an object of your choosing. The EJS associative array index mechanism is an efficient hash lookup.
For example:
var store = new Array(100);
store["product 1"] = new Product();
...
for (product in store) {
print("Product is " + store[product].id);
}
The for / in statement iterates over all the elements of the array, setting the index variable "product" to each element in the store array.
Numerical Arrays
Numerical arrays store arbitrary data indexed by numerical values with an origin at zero. Numerical arrays are created via the new operator and the Array() constructor. The Array constructor supports invocation with no parameters, a single size parameter and a list of elements to insert into the array. For example:
var empty = new Array(); // Create an empty array
var ten = new Array(10); // Create an empty array with 10 slots
var products = new Array("milk", "eggs", "bread");
When an array is created the
length property is set to the current length of the array. When an array is created with a single size parameter, it's length will be set to the requested size and all the elements in the array will be set to undefined.
EJS does not support array literals.