...
Like most JavaScript frameworks, we simulate classes by a constructor function and its prototype chain.
The ES5 API Object.create(
prototype,
propertyDescriptors)
serves this task very well.
While members are properties of the constructor function prototype, static members are properties of the constructor function itself.
Non-public members are discussed in a dedicated sub-section.
Class structure
The translation pattern is a follows:
...
The constructor
property has to be defined explicitly, as otherwise, a subclass would inherit that property from its superclass, too, resulting in the subclass having the same constructor as its superclass.
Members and visibility (public, protected, internal, private)
Only public and protected members should be stored as properties of the constructor function / its prototype as-is. For protected members, access rights are checked by the compiler.
For private and internal members, we have to avoid name-clashes. There are different cases:
- Private methods (non-static as well as static ones) and private static fields are declared in new scope, shared by all class members. Thus, they are visible for every method, but not from outside. Non-static private method calls have to be rewritten to run with the correct
this
. - Private non-static fields are renamed: they are post-fixed by
$
plus the inheritance level of the declaring class, so that they cannot name-clash with fields of the same name, declared in a subclass or superclass. - Internal members are post-fixed by
$
plus the full package name (with.
replaced by_
), so that they cannot name-clash with members of the same name, declared in a subclass or superclass residing in another package.
Interfaces plus "as" and "is" operator
...
"this" is always in scope
TODO
...
TODO
Statements
TODO
for each
...