Debian Codegraph @

.cargo/vendor/wasm-bindgen/guide/src/reference/attributes/on-rust-exports/readonly.md

raw
1 # `readonly`
2
3 When attached to a `pub` struct field this indicates that it's read-only from
4 JavaScript, and a setter will not be generated and exported to JavaScript.
5
6 ```rust
7 #[wasm_bindgen]
8 pub fn make_foo() -> Foo {
9 Foo {
10 first: 10,
11 second: 20,
12 }
13 }
14
15 #[wasm_bindgen]
16 pub struct Foo {
17 pub first: u32,
18
19 #[wasm_bindgen(readonly)]
20 pub second: u32,
21 }
22 ```
23
24 Here the `first` field will be both readable and writable from JS, but the
25 `second` field will be a `readonly` field in JS where the setter isn't
26 implemented and attempting to set it will throw an exception.
27
28 ```js
29 import { make_foo } from "./my_module";
30
31 const foo = make_foo();
32
33 // Can both get and set `first`.
34 foo.first = 99;
35 console.log(foo.first);
36
37 // Can only get `second`.
38 console.log(foo.second);
39 ```