Fun with Stamps. Episode 25. Getters and setters built-in support

Vasyl Boroviak
2 min readSep 15, 2019

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!

--

--