Properties

Class properties

A class declaration can have default values set, if no other values are set at instantiation they will stay as they are.

import $Class from "@Class";

/**
 * @property {string} type - the type of the Class
 */
var MyClass = $Class.declare("MyClass", {
    properties: {
        type: "MyClass"
    }
});

// A class that inherits from a superclass can refine its properties.
/**
 * @extends MyClass
 * @inheritdoc
 */
var MyChildClass = $MyClass.declare("MyChildClass", {
    properties: {
        type: "MyChildClass"
    }
});

var myClass = new MyClass();
console.log(myClass.type); // MyClass

// At instantiation it's also possible to override the properties values
var myOtherClass = new $MyClass({ type: "MyCoolClass" });
console.log(myOtherClass.type); // MyCoolClass

var myChildClass = new MyChildClass();
console.log(myChildClass.type); // MyChildClass

The constructor has access to the resolved properties, and can override their values.

Data type

Primitives

The type of property can be any of the primitives supported by javascript:

  • undefined
  • null
  • boolean
  • number
  • string

Object

Binding

The property value can be a function, named a binding function. A binding function let you compute the value of the property with a function, based on other properties values. Dynamic properties can be declared as functions or as arrow expressions:

  • myProp: function () { return this.myOtherProp; }
  • myProp: ({myOtherProp}) => { return myOtherProp}
  • myProp: ({myOtherProp}) => myOtherProp

All declarations above result to ‘myProp’ having the same value as ‘myOtherProp’. When ‘myOtherProp’ change, ‘myProp’ will automatically be updated when we will try to get its value.

Note

  • For a function declaration, the this keyword is mandatory to access class instance properties.
  • For an arrow expression declaration, the this keyword is forbidden as it won’t set the right context

Properties options

To define a property option, you must define the property by using its class. The following properties (exclusive) are supported:

Property nameUsageDefault value
readOnlyAllows to disable direct set (error thrown) but property is bindablefalse
immutableAllows to define property as immutable (getter and setter disable, no watch)false

Did you know?

A class using a readOnly property can change the value on the property internally (inside the class) by using $Property.setCacheValue() function. $Property.setCacheValue() must only be used in private set pattern (not outside the class)

var MyClass = $Class.declare("MyClass", {
    properties: {
     myProp: $Property("myProp", {readonly: true})
    }
});
var myClass = new MyClass();
myclass.myProp = "anotherValue"; // will raise an error