Custom setters and getters
Sometimes it may be necessary to have custom setters and/or getters for some attributes. Structure allows you to do that using native JavaScript setters and getters. It will even support coercion.
It's important to notice that you should not try to access the attribute directly inside its getter or to set it directly inside its setter because it will cause infinite recursion, this is default JavaScript behavior. To access an attribute value inside its getter you should use this.get(attributeName)
, and to set the value of an attribute a setter you should use this.set(attributeName, attributeValue)
:
Inheritance
Custom setters and getters are also inherited, be your superclass a pure JavaScript class or another structure:
Important
JavaScript nativelly won't let you inherit only one of the accessors (the getter or the setter) if you define the other accessor in a subclass:
It happens because once you define one of the accessors in a subclass, all the accessors for the same attribute inherited from the superclass will be ignored.
While it's a weird behavior, Structure will follow the same functionality so the Structure classes inheritance work the same way of pure JavaScript classes, avoiding inconsistencies.
Last updated