方法
method
屬性允許您描述匯入 JavaScript 物件的方法。它應用於具有 this
作為其第一個參數的函式,該參數是對匯入 JavaScript 型別的共享參考。
# #![allow(unused_variables)] #fn main() { #[wasm_bindgen] extern "C" { type Set; #[wasm_bindgen(method)] fn has(this: &Set, element: &JsValue) -> bool; } #}
這會在 Rust 中的 Set
上產生一個 has
方法,該方法會在 JavaScript 中呼叫 Set.prototype.has
方法。
# #![allow(unused_variables)] #fn main() { let set: Set = ...; let elem: JsValue = ...; if set.has(&elem) { ... } #}