documentationfor yFiles for HTML 2.6

Class<T>

Instances of the class Class represent classes and interfaces in a running yFiles application.

Inheritance Hierarchy
Class

Remarks

This class gives reflective access to a type's attributes and properties. It also provides some static utility methods like ensure and fixType.

At runtime, a Class object can be obtained through the static $class property on yFiles types or the getClass() method on instances. The class framework automatically creates a Class object for all JavaScript classes that inherit from a yFiles type. It also adds the $class and getClass members.

Note that the class framework creates the Class object for classes that inherit from yFiles classes only when the first instance of this subclass is created. If the $class property is required before an instance of the subclass was created, you need to call fixType first.

This class may also be used as a function to create a yFiles class from a class definition object.

There are only very few cases where you will actually need to use this function to create a class. The yFiles class framework should be compatible with all major popular techniques of creating classes and instances. You may use prototypal inheritance, ES2015 classes, TypeScript classes and other techniques that use the same mechanics for inheritance.

The class definition object may contain any of the following special identifiers:

  • $extends: An object reference that points to the parent object.
  • $with: An array of interfaces this class implements.
  • $abstract: A boolean value that indicates whether or not this class is abstract.
  • $static: An object that contains methods, fields and properties that should be statically available and will be registered on the constructor function instead of its prototype.
  • $meta: An array of Attributes or a function that returns an array of Attributes that describe the class.

Additionally, it may contain methods, properties and fields that should be added to the constructor functions prototype. If the value of an field is an object with a $meta property, then it will be used as a property descriptor for the field (meaning that writable, enumerable and configurable, value and get, set can be used as in a property descriptor for Object.defineProperty and the value of the $meta property is stored for usage in the reflection API. If the object has a get and/or set property, then it will also be used as a property descriptor.

var MyList = yfiles.lang.Class('MyList', {
    $extends: my.AbstractList,
    $with: [my.Iterable, my.Collection],

    constructor: function() { // the constructor function
    },

    add: function(obj) { ... },
    size: { // transformed into a property
     'get': function() { return ...; }
    },

    $static: {
     create: function() { ... }
    }
});
// usage
var list = new MyList();
list.add("hello");
list.size === 1; // true

The constructor property can be either a function (in case of a single constructor) or an object that is transformed into named constructors.

var Rect = yfiles.lang.Class('Rect', {
 constructor: {
     'default': function(x, y, width, height) { ... },
     'FromPoints': function(topLeft, bottomRight) { ... },
     'FromPoint': function(topLeft, width, height) { ... }
 }
});
// usage:
var myrect = new Rect(10, 10, 50, 50);
myrect instanceof Rect; // true
myrect = new Rect.FromPoints({x: 10, y: 10}, {x: 60, y: 60});
myrect instanceof Rect; // true
myrect = new Rect.FromPoint({x: 10, y: 10}, 50, 50);
myrect instanceof Rect; // true

Named constructors can call other constructors and can be written like any other constructor, i.e. this will be a new instance of the class that is being constructed.

To access super methods, you can use the static $super property that is added to all classes.

Example (inside a method or property of the previously defined Rect class):

Rect.$super.toString.call(this);

If you do not specify the parent class of a new class, then YObject will be used as its base type. The parent class of YObject is the standard JavaScript Object.prototype.

Every class has a couple of static methods and properties:

  • isInstance: tests whether an object is an instance of this class.
  • $class: a reference to the Class<T> instance that provides reflective information about this type.
  • $super: the parent class.

Type Parameters

T
The type of the class which is represented by this .

Type Details

yfiles module
lang
yfiles-umd modules
All modules
Legacy UMD name
yfiles.lang.Class

See Also

Properties

Methods

Static Methods