Most programming languages implement primtive types to determine the data type of a variable, for example string, boolean, object. In JOII you can predefine the type of a class member using metadata.

JOII supports the following primitive types as they are used in JavaScript:

  • string
  • boolean
  • number
  • object
  • function
  • const

Class properties are not accessible from the outside of a class, but JOII generates associated getters and setters for you, depending on the visibility setting of the property.

Getters of boolean-types are prefixed with "is" instead of "get".

Example #1: Primitive Types

var PrimitiveTypes = Class({

    'const MY_CONSTANT' : 'Hi there!',

    'public string  string_type'  : 'Hello World',
    'public number  number_type'  : 42,
    'public boolean boolean_type' : true,
    'public object  object_type'  : { foo: 'bar' },

    'public function someFunction()' : function() {
        return this.getNumberType();


var pt = new PrimitiveTypes();

pt.MY_CONSTANT;     // 'Hi there!'
pt.getStringType(); // 'Hello World'
pt.getNumberType(); // 42
pt.isBooleanType(); // true
pt.getObjectType(); // { foo: bar }
pt.someFunction();  // 42

If no type is specified, any data-type is allowed.

Example #2: Using setters of typed properties

// Continuing from Example #1...

pt.setStringType('This is allowed');
pt.setStringType(1234); // Exception: setStringType expects a string, number given.
pt.setNumberType(null); // Exception: setNumberType expects a number, null given.

Nullable types

In some scenarios you want a property to be nullable. This means to allow null to be passed as an argument in the associated setter. This is easily achieved by setting the nullable flag in the property metadata.

Example #3: Nullable types

var PrimitiveTypes = Class({
    // ...
    'public nullable number number_type'  : 42,
    // ...

var pt = new PrimitiveTypes();

// This is now allowed:

Immutable types

There are also situations where a property should be read-only by the public. This is done using the immutable or read flag. When this flag is applied to the metadata of a property, only a getter is generated.

There is no difference in the flags immutable and read.

Example #4: Immutable types

var PrimitiveTypes = Class({
    // ...
    'public immutable number number_type'  : 42,
    // ...

var pt = new PrimitiveTypes();

pt.getNumberType(); // 42

// Error: setNumberType is undefined.

Custom types

This is where things get interesting. JOII allows you to use a class or interface definition as a type in a property declaration. When the setter method is called for this property, JOII will automatically validate the passed argument to check if the given instance matches the correct type.

Remember to define an internal name for your object by passing a string to the first argument of Class() or Interface(). This name will be known as your custom type which can then be used in property definitions.

Example #5: Using custom types

var IFoo = Interface('Foo', {});

var Foo = Class({ implements: IFoo }, {
    // class body...

var MyClass = Class({
    'public nullable Foo foo_a': null,
    'public          Foo foo_b': null

var mc = new MyClass();

mc.setFooA(new Foo()); // OK
mc.setFooA(1234);      // Exception: setFoo1 expects an instance of IFoo, number given.
mc.setFooA(null);      // OK, because the property is nullable.

mc.setFooB(new Foo()); // OK
mc.setFooB(null);      // Exception: setFoo2 expects an instance of Foo, null given.