JavaScript Mapping for Structures

On this page:

Basic Mapping for Structures

A Slice structure maps to a JavaScript type with the same name. For each Slice data member, the JavaScript instance contains a corresponding property. As an example, here is our Employee structure once more:

Slice
struct Employee
{
    long number;
    string firstName;
    string lastName;
}

The mapping for this structure is equivalent to the following code:

JavaScript
class Employee
{
    function(number = new Ice.Long(0, 0), firstName = "", lastName = "")
    {
        this.number = number;
        this.firstName = firstName;
        this.lastName = lastName;
    }
}
TypeScript
class Employee
{
    constructor(number?:Ice.Long, firstName?:string, lastName?:string);
    clone():Employee;
    equals(rhs:any):boolean;
    hashCode():number;
    number:Ice.Long;
    firstName:string;
    lastName:string;
    static write(outs:Ice.OutputStream, value:Employee):void;
    static read(ins:Ice.InputStream):Employee;
}

For each data member in the Slice definition, the constructor assigns a value to its corresponding property. Instances define an equals method for comparison purposes and a clone method to create a shallow copy. If the structure qualifies for use as a dictionary key, instances also define a hashCode function as required by the Ice.HashMap type.

 

JavaScript Constructors for Structures

The generated constructor has one parameter for each data member. This allows you to construct and initialize an instance in a single statement (instead of first having to construct the instance and then assign to its members). The constructor assigns a default value to each property appropriate for the type of its corresponding member:

Data Member TypeDefault Value
stringEmpty string
enumFirst enumerator in enumeration
structDefault-constructed value
NumericZero
boolFalse
sequenceNull
dictionaryNull
class/interfaceNull

If you wish to ensure that data members of primitive and enumerated types are initialized to specific values, you can declare default values in your Slice definition. The constructor initializes each of these data members to its declared value instead.

See Also