This page describes some of the common types used in mXrap JavaScript.

Number, Text, Boolean

JavaScript has many data types. The basic types are Number, Text, Boolean

const length = 16;                               // Number
const value = -15.312;                           // Number (with decimals)
const rockType = "granite";                      // Text (String)
const isPit = true;                              // Boolean
const isBore = false;                            // Boolean

Array

An array is a list containing items. They are written with square brackets, and array items are separated by commas.

const rockTypes = ["granite", "shale", "clay"];  // array of Text (String)
const values = [ 5, 6.75, 7.22 ];                // array of Numbers
const isRange = [ true, false, true, true ];     // array of Booleans

The length of an array can be queried from its length property. However in mXrap JS it is common to reuse arrays, hence the length of the array may not always reflect the intended number of items to be used in the current context. Hence most mXrap JS library functions that take in input arrays will also ask for the length of the array as a separate parameter. Example:

const x_array = [1, 2, 3, 4, 5];
const y_array = [7, 8, 4, 10, 11];
const bestfitter = XFit.BestFit.linear_array(x_array, y_array, 5); // 5 as the number of points to use in the x_array and y_array arrays.

You can access the items in the array with:

x_array[0]
...
x_array[n-1]

, where n is the size of the array

Point

A Point is an array with three elements [x, y, z], which denotes the array refers to a point in the grid/space. Sometimes when referring to a vector, the array will be referred to as a "vec3" rather than as a point.

glMatrix

A glMatrix is an array. It comes from the glMatrix library for fast vector and matrix mathematics, which several of mXrap's Javascript libraries use. Rather than represent a matrix (e.g. 3x3) as an array of arrays (which can be rather inefficient in terms of overhead), it represents the matrix as a single array. For example, the 3x3 matrix:

[ 1 , 4 , 7 ]
[ 2 , 5 , 8 ]
[ 3 , 6 , 9 ]
is represented by the "glMatrix" [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ].

Function

A JavaScript function is a block of code that performs a specific task. It can take in one or more parameters and return a value.

For example, 
const addTo2 = function(x) {
   return x + 2;
}

print(addTo2(12)); // prints 14

Javascript functions may sometimes be an input to or output from mXrap JS libraries. For example, the XFit.Result.generateFunction ( bestfitter ) function returns a function which computes the fitted equation for an input x.

const generatedFunction = XFit.Result.generateFunction (bestfitter);
for(let i = 0; i < 10; i = i + 1) {
   print(generatedFunction(i));    // prints the fitted value for x = 0, 1, 2, ... 9;
}  

When used with mXrap Tables, they often refer to the read or write functions of the Table. You should look at the TableFunction section if this is the case.

Object

An object can be thought of as a bundle or container that contains a group of named values.

const parametersObj = { mean: 15, stdev, 3.56, distribution: "Normal" };

You can attach values to an object. We usually call this a member or property of the object.

parametersObj.mean = 12;         // sets or updates the property
parametersObj["mean"] = 12;      // an alternative syntax

print(parametersObj.mean);       // prints 12
print(parametersObj["mean"]);    // prints 12

You can also attach a function to an object. We usually call this a method of the object.

parametersObj.addTo2 = function(x) {
   return x + 2;
}
print(parametersObj.addTo2(12));  // prints 14

You can check what's attached to the object by iterating through its contents.

for(let memberName in parametersObj) {
   print(memberName, ":", parametersObj[memberName]);
}

mXrap Classes are Objects too

Note that all the "class" types that are specified in mXrap JS libraries are also objects. The difference in having a class type is merely one of documentation, when we define a class type we are saying - this is an Object, but by intent of the library it will usually have the documented properties and methods.

Library

Similarly, a mXrap JS library is also an Object. When you load a library, e.g.:

const XShapes = load('js/standard/xshapes.js');
You are loading an instance of the XShapes library, an Object, into the variable called XShapes, from which you call the associated functions.

Table

When you import a input table, or create an output table, in a mXrap table calculation, a variable referring to the table's javascript name is automatically available in the table calculation.
You can see the list of table objects, and their methods, in the "Data API" tab of the table calculation.

An input table generally has the following methods:

  • size() - this returns the size of the table.
  • read_functions

An output table generally has the following methods:

  • size() - this returns the current size of the table.
  • reserve(n) - this reserves n rows for the table (reserving does not increase the size of the table immediately, but tells mXrap to prepare the resources)
  • grow(n) - this grows the current size of the table by n rows
  • resize(n) - this resizes the current size of the table to n.
  • read_functions
  • write_functions

read_functions and write_functions

For example, we choose to import the Table "!/Data Tables/Events" as Events, with the input columns "id", "localMagnitude", "totalRadiatedEnergy".
This means the table object "Events" is available, as are the read functions:

Events.read_id(i);
Events.read_localMagnitude(i);
Events.read_totalRadiatedEnergy(i);

here i refers to the index in the table, it starts from 0 and ends at the size of the table - 1.

We also choose to create an output Table "Counts", with the output columns "ID", "Type" and "Count".
This means the table object "Counts" is available, as are the write functions:

Counts.write_ID(i, id);
Counts.write_Type(i, type);
Counts.write_Count(i, count);

here i refers to the index in the table, it starts from 0 and ends at the size of the table - 1.