---
title: "Classes"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes
language: js
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Classes

Programming language: JavaScript. Other languages: [C++](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/java.md), [MATLAB](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/php.md), [Python](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/swift.md).

A class is a user-defined type that holds a list of fields, like a [struct](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/js.md). You define a class with the `class` keyword:

```slice
module M
{
    class TimeOfDay
    {
        short hour;         // 0 - 23
        short minute;       // 0 - 59
        short second;       // 0 - 59
    }
}
```

Unlike a struct, a class can be empty.

Classes offer capabilities that structs do not:

- **Extensibility** You can extend a class through [inheritance](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/class-inheritance.md) and [optional fields](https://docs.zeroc.com/ice/3.8/slice/fields/js.md#optional-fields).
- **Graph preservation** Ice preserves the references between class instances: two references to the same instance in a [class graph](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/self-referential-classes/js.md) still refer to one instance after unmarshaling.
- **Null value** A class parameter or field can be null, whereas a struct parameter or field cannot.
- **Slicing** With the [sliced format](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slicing-values-and-exceptions/js.md#slice-formats), a receiver can unmarshal an instance of a derived class it does not know as the most-derived base class it knows.

Marshaling a class takes more time and space than marshaling a struct, so use a class only when you need one of these capabilities.

> **Note**
>
> A class represents data that you transmit over the wire, just like a struct. You can't define operations on a Slice class or implement an interface with a Slice class.

## Language Mapping

A Slice class is mapped to a JavaScript class with the same name. For each Slice field, the JavaScript instance contains a corresponding field (just as for structures and exceptions). Consider the following class definition:

```slice
class TimeOfDay
{
    short hour;         // 0 - 23
    short minute;       // 0 - 59
    short second;       // 0 - 59
    string tz;          // e.g. GMT, PST, EDT...
}
```

The Slice compiler generates the following code for this definition:

```js
// JavaScript generated code.
class TimeOfDay extends Ice.Value {
    constructor(hour = 0, minute = 0, second = 0, tz = "") {
        super();
        this.hour = hour;
        this.minute = minute;
        this.second = second;
        this.tz = tz;
    }
}
```

```typescript
// TypeScript generated definition.
class TimeOfDay extends Ice.Value {
    constructor(hour?: number, minute?: number, second?: number, tz?: string);

    hour: number;
    minute: number;
    second: number;
    tz: string;
}
```

There are a number of things to note about the generated code:

1. The generated `TimeOfDay` class inherits from `Ice.Value`.
2. The generated class provides a constructor that accepts a value for each field.
3. The generated class defines a JavaScript field for each Slice field.

### Generated Constructor

The generated constructor has one parameter for each field. 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 fields).

For example:

```js
const tod = new TimeOfDay(14, 45, 0, "PST"); // 2:45pm PST
```

All these parameters have default values (see [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/js.md)).

For a derived class, the constructor accepts an argument for every field of the class, including inherited fields. For example, consider the definition from [Class Inheritance](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/class-inheritance.md) once more:

```slice
class TimeOfDay
{
    short hour;         // 0 - 23
    short minute;       // 0 - 59
    short second;       // 0 - 59
}

class DateTime extends TimeOfDay
{
    short day;          // 1 - 31
    short month;        // 1 - 12
    short year;
}
```

The constructors generated for these classes are similar to the following:

```js
class DateTime extends TimeOfDay {
    constructor(hour, minute, second, day = 0, month = 0, year = 0) {
        super(hour, minute, second);
        this.day = day;
        this.month = month;
        this.year = year;
    }
}
```

```typescript
class DateTime extends TimeOfDay {
    constructor(
          hour?: number,
          minute?: number,
          second?: number,
          day?: number,
          month?: number,
          year?: number);

    day: number;
    month: number;
    year: number;
}
```
