Home
This library offers declarative approach to supporting protected and private members in ES6 classes, as opposed to hiding values physically, as done by most libraries.
What this means is that it does not modify your class at all. Instead, it uses the basic inheritance principle to extend your class with easy access to protected and private namespaces.
The difference from the libraries that do override the class structure, is that while protected and private members are also hidden by the base class, they are not hidden physically, they can still be accessed, but to do so requires explicit access to the hidden namespaces $protected
and $private
.
To put it otherwise, the library hides all the protected and private properties (they are not enumerable), but not from hacking into them, it considers the declarative approach to implementing protected/private class members to be sufficient.
this.$protected
gives access to the protected namespace of the class hierarchy.
Protected namespace is shared across all classes in the derivation hierarchy.
class Parent extends AccessSpecifier {
constructor() {
super();
let a = this.$protected; // access protected namespace
a.msg = 'Hello World!';
}
}
class Child extends Parent {
constructor() {
super();
let a = this.$protected; // access protected namespace
a.msg = 'New Message'; // override what class Parent set
}
}
this.$private(class)
gives access to the private/isolated namespace of the class.
class Parent extends AccessSpecifier {
constructor() {
super();
let p = this.$private(Parent); // access default private namespace of the class
p.msg = 'Hello World!'; // isolated value of the class
}
}
class Child extends Parent {
constructor() {
super();
let p = this.$private(Child); // access default private namespace of the class
p.msg = 'New Message'; // isolated value of the class
}
}
You can in fact pass in any value into method $private
, to access any number of private namespaces within the class. But the caution is advised not to create such namespaces that may be in use by other classes in the hierarchy, which would create a conflict.
And if you call it without parameters - this.$private()
, it will give access to the default private namespace of the class at the bottom of the derivation hierarchy, i.e. default means that class uses it as this.$private(ClassName)
.
A sealed class cannot be extended. Any attempt to extend such a class will throw an error during the construction.
class Parent extends AccessSpecifier {
constructor() {
super();
this.sealClass(Parent); // prevent any derivation from this class
}
}
class Child extends Parent {
constructor() {
super(); // will throw TypeError: Class 'Parent' is non-extendable!'
}
}
Properties available from class AccessSpecifier
:
-
className
- name of the bottom-level class in the derivation hierarchy -
isAbstract
- settrue
when creatingAccessSpecifier
directly, asnew AccessSpecifier()
, rather than through inheritance.
Alternative libraries that implement private properties by overriding the class: