Using Objects, Properties, and Methods

An object is a self-contained module of data and its associated processing. We get objects to do work, or retrieve things for us, by calling the methods of the objects. We can also retrieve things from an object using the object’s properties. A method is like a function provided to us by an object. When we write script that asks for an object to run a particular method, we say we are calling a method. You can call a method in the following way:

objectName.methodName(parameters);

Sometimes a method returns a value and that return value is a variable in your script, in which case you call the method this way:

myVariable = objectName.methodName(parameters);

A property is like a variable that is part of an object. You can always retrieve a property, but you cannot usually change the property. You can retrieve a property value as follows:

myVariable = objectName.propertyName;

Some objects are available to your script at all times, like the aa object. You retrieve other objects through method calls, or create objects directly by your script like in the examples that use an array. Several predefined objects are available to script writers. Some of these, like Array, Math, and String are part of the JavaScript language. Other predefined objects like aa are additions to JavaScript provided by Accela for interacting with Civic Platform.

Topics:

The Array Object

We have already seen one example of how to create an array, but there are other ways to create an array that can be more convenient. An example:

myArray = new Array(“Oranges”, “Bagels”, “Spinach”); 
aa.print(myArray[0]); 
aa.print(myArray[1]); 
aa.print(myArray[2]); 

This script displays this output:

Oranges 
Bagels 
Spinach 

In this example, we initialize an array simultaneously with three elements. This approach provides an easy way to create a small array when you know the contents of that array. The Array object has the property length, and the methods concat, join, pop, push, reverse, shift, slice, splice, sort, and unshift among others.

The Math Object

This object provides access to most if not all of the mathematical functions that you might need when writing scripts. The object defines properties such as E, LN10, LN2, PI and others. Recognize PI as the familiar constant 3.14159. The other properties are also constants. The Math object defines many constants not already mentioned. The Math object also defines these methods: abs, acos, asin, atan, atan2, ceil, cos, exp, floor, log, max, min, pow, random, round, sin, sqrt, and tan. Let us look at an example of using the math object:

piToTheThirdPower = Math.exp(Math.PI, 3); 
aa.print(piToTheThirdPower); 

This script displays this output:

23.140692632779267

The example calls the “exp” method of the Math object, passes in the PI property of the Math object as the first parameter of the method, and three as the second parameter of the method. The exp method takes its first parameter and raises it to the power of the second parameter. The output is 3.14159 * 3.14159 * 3.14159 = 23.140692632779267. Consult the Civic Platform Script Writer’s Object Model Reference documentation or a book on JavaScript for more information on the Math object.

The String Object

When you execute a script with the line:

myVariable = “Hello World.”

You are really creating a String object. Let us look at an example:

myString = “Hello World”; 
aa.print(myString.length); 
aa.print(myString.toUpperCase()); 

This script displays this output:

11 
HELLO WORLD 

We can see from the example that you can use the name of the variable that contains the string to retrieve the length of the string, and call a method of the String object that retrieves an upper case version of a string. The String object has a length property, and the methods slice, split, substr, substring, toLowerCase, and toUpperCase among others. Consult the Civic Platform Script Writer’s Object Model Reference or a book on JavaScript for more information on the String object.