Debian Codegraph @

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

raw
1 # `js_name = Blah`
2
3 The `js_name` attribute can be used to export a different name in JS than what
4 something is named in Rust. It can be applied to both exported Rust functions
5 and types.
6
7 For example, this is often used to convert between Rust's snake-cased
8 identifiers into JavaScript's camel-cased identifiers:
9
10 ```rust
11 #[wasm_bindgen(js_name = doTheThing)]
12 pub fn do_the_thing() -> u32 {
13 42
14 }
15 ```
16
17 This can be used in JavaScript as:
18
19 ```js
20 import { doTheThing } from './my_module';
21
22 const x = doTheThing();
23 console.log(x);
24 ```
25
26 Like imports, `js_name` can also be used to rename types exported to JS:
27
28 ```rust
29 #[wasm_bindgen(js_name = Foo)]
30 pub struct JsFoo {
31 // ..
32 }
33 ```
34
35 to be accessed like:
36
37 ```js
38 import { Foo } from './my_module';
39
40 // ...
41 ```
42
43 Note that attaching methods to the JS class `Foo` should be done via the
44 [`js_class` attribute](js_class.html):
45
46 ```rust
47 #[wasm_bindgen(js_name = Foo)]
48 pub struct JsFoo { /* ... */ }
49
50 #[wasm_bindgen(js_class = Foo)]
51 impl JsFoo {
52 // ...
53 }
54 ```