Fun with Stamps. Episode 25. Getters and setters built-in support
Hello. I’m developer Vasyl Boroviak and welcome to the twenty fifth episode of Vasyl Boroviak presents Fun with Stamps.
TL;DR: Now you can use JavaScript getters and setters as if they are regular JavaScript object properties:
const MyInfo = stampit({
props: {
_info: "_initial_"
},
methods: {
get info() {
return `getter_${this._info}`;
},
set info(info) {
this._info = `PREFIX+${info}`;
},
}
});
const object = MyInfo();object._info === "_initial_";
object.info === "getter__initial_";object.info = "ttttt";object._info === "PREFIX+ttttt";
object.info === "getter_PREFIX+ttttt";
As you can see from the code above the getters and setters work as expected.
Available now in stampit@4.3.0
. See docs at https://stampit.js.org
To make this happen we released a new version of stamp specification — v1.6. It now declares that the standard compose
function must copy getters and setters across with all other metadata of the stamp descriptor.
Downside
There is one downside from that feature though. The object instance creation performance dropped by 5 to 10 times. My laptop creates 0.5 million object instances from a stamp, and about 30 million object instances from a similar class.
Have fun with stamps!
The rest of the episodes:
- Episode 1. Stamp basics
- Episode 2. Dependency injection in FP
- Episode 3. Comparing with the ES2015 classes
- Episode 4. Implementing stamps yourself in 30 LOC
- Episode 5. Composition design pattern
- Episode 6. Statics — properties on stamps
- Episode 7. Early and late dependency injection
- Episode 8. Tracking and overriding composition
- Episode 9. Detaching compose()
- Episode 10. My stamp mental model
- Episode 11. Interfering composition
- Episode 12. New @stamp home
- Episode 13. Method collision control
- Episode 14. New @stamp/it as a replacement of Stampit
- Episode 15. The @stamp/ modules ecosystem
- Episode 16. TypeScript mix-in classes vs Stamps
- Episode 17. Easy 100% unit test coverage in JS
- Episode 18. Dependency injection paradise
- Episode 19. Java/C# abstract methods in JavaScript
- Episode 20. Stampit v4
- Episode 21. Private data in JavaScript. 4 ways using stamps
- Episode 22. JavaScript instanceof as composable stamp
- Episode 23. New stampit.js.org with all the docs
- Episode 24. New “name” feature
- Episode 25. Getters and setters built-in support (this article)