JavaScript Mapping for Structures

On this page:

Basic JavaScript 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:

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

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

var Employee = function(number, firstName, lastName)
    this.number = ...
    this.firstName = ...
    this.lastName = ...

For each data member in the Slice definition, the JavaScript 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

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