Instances of the class Class
represent classes and interfaces in a running yFiles application.
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
Returns all attributes that belong to this type.
Parameters
A map of options to pass to the method.
- inherited - boolean
- If
true
, then the attributes of the parent types are also added to the list. Default value isfalse
.
Returns
- ↪Attribute[]
- An array of attributes that belong to this type.
Returns an array of all properties of the type.
Returns
- ↪PropertyInfo[]
- An array of properties.
Tests whether an object is an instance of this type.
Static Methods
Makes sure the yFiles for HTML modules that contain the given types are not stripped away by build optimizers like tree-shaking tools.
Remarks
Sometimes you want to load yFiles types without using them explicitly in your code, for example the styles used in a GraphML files or implicit dependencies between different styles of the automatic layout. If you just import the corresponding module without using any of its types, a build optimizer might remove this seemingly unused import. To avoid this, you can pass the type to this method.
Note that yFiles shows a clear error message if a type is missing due to an implicit dependency. You don't have to call this method for types that you're using explicitly.
This is an empty function without any code.
Parameters
A map of options to pass to the method.
- types - Function
- A list of types.
See Also
Makes sure the prototype of this type is properly set up and $class and getClass() is working as expected.
Remarks
Parameters
A map of options to pass to the method.
- type - Function
- The type
- name - string
- The optional (short) name to use for class. This will be returned by getClass()/$class.name. This will be guessed from the function name if unspecified.
Returns the class object for the given name, or null
if no such class exists.
Type Parameters
- T
- The type of the class which is represented by this
.
Parameters
A map of options to pass to the method.
- name - string
- The name of the requested class.
Returns
- ↪Class<T>
- The class object or
null
, if no such class exists.