mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-07-02 14:11:32 +00:00
guide: add exhuastive reference docs for #[wasm_bindgen] attributes
This commit is contained in:
26
guide/src/reference/attributes/on-js-imports/method.md
Normal file
26
guide/src/reference/attributes/on-js-imports/method.md
Normal file
@ -0,0 +1,26 @@
|
||||
# `method`
|
||||
|
||||
The `method` attribute allows you to describe methods of imported JavaScript
|
||||
objects. It is applied on a function that has `this` as its first parameter,
|
||||
which is a shared reference to an imported JavaScript type.
|
||||
|
||||
```rust
|
||||
#[wasm_bindgen]
|
||||
extern {
|
||||
type Set;
|
||||
|
||||
#[wasm_bindgen(method)]
|
||||
fn has(this: &Set, element: &JsValue) -> bool;
|
||||
}
|
||||
```
|
||||
|
||||
This generates a `has` method on `Set` in Rust, which invokes the
|
||||
`Set.prototype.has` method in JavaScript.
|
||||
|
||||
```rust
|
||||
let set: Set = ...;
|
||||
let elem: JsValue = ...;
|
||||
if set.has(&elem) {
|
||||
...
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user