Classes
Classes in yFiles for HTML behave like most developers would expect them to. They have the same semantics that ES2015 classes, C# classes, TypeScript classes, and Java classes use. Classes in the public API can have a constructor and if they don’t have a public constructor they cannot be instantiated by custom code. Class instances may have instance members like methods, which are just functions on the prototype of the class, properties with either or both of setter and getter accessors, as well as simple fields.
Classes can have static members, too. Static members are available on the class itself.
Extending Classes
Extending a class from the yFiles for HTML API is straightforward and does not require using the class framework infrastructure. In order to derive a custom subclass from a yFiles for HTML API class, different well-known and established techniques can be used. Which one works best for you depends on what language level you are targeting and what tools you are using for authoring your code.
yFiles for HTML supports these techniques for subclassing as well as all technologies that are based on them:
- ES2015
class
keyword style inheritance - TypeScript
class
keyword inheritance - Using the yFiles for HTML class framework
- prototypal inheritance known from ECMAScript Level 5 and older “classy frameworks”
We recommend using the class
keyword support for concise subclassing code. If your application needs to target ECMAScript Level 5 browser
implementations, take a look at the various available source-code transpiler options that let you author code using
ES2015 and run on ECMAScript Level 5-compatible browsers.Using ES2015 classes at development time has the advantage that you can run your code without a transpiler step in all
modern browsers during development.
Extending an existing class using ES2015 class
works just as you would expect it to work:
This is almost identical to extending a class using TypeScript’s class
keyword:
Starting with yFiles for HTML 2.0.0.1 the TypeScript compiler can be set to either target ECMAScript Level 5 or ES2015.
Using the class support from the yFiles for HTML class framework is compatible with ECMAScript Level 5 and results in a concise declaration:
For more detailed background information about class declarations in the class framework, please see the section about classes in the class framework.
Since the underlying technology is compatible with ECMAScript Level 5 prototypal inheritance, this is also an option: