getter
and setter
The getter
and setter
attributes can be used in Rust impl
blocks to define
properties in JS that act like getters and setters of a field. For example:
Can be combined in JavaScript
like in this snippet:
const obj = new Baz(3);
assert.equal(obj.field, 3);
obj.field = 4;
assert.equal(obj.field, 4);
You can also configure the name of the property that is exported in JS like so:
Getters are expected to take no arguments other than &self
and return the
field's type. Setters are expected to take one argument other than &mut self
(or &self
) and return no values.
The name for a getter
is by default inferred from the function name it's
attached to. The default name for a setter
is the function's name minus the
set_
prefix, and if set_
isn't a prefix of the function it's an error to not
provide the name explicitly.