js_class = Blah
js_class
属性用于指示 impl
块内的所有方法都应附加到指定的 JS 类,而不是从 impl
块中的 self 类型推断。js_class
属性最常与结构体上的 js_name
属性 结合使用
#![allow(unused)] fn main() { #[wasm_bindgen(js_name = Foo)] pub struct JsFoo { /* ... */ } #[wasm_bindgen(js_class = Foo)] impl JsFoo { #[wasm_bindgen(constructor)] pub fn new() -> JsFoo { /* ... */ } pub fn foo(&self) { /* ... */ } } }
访问方式如下
#![allow(unused)] fn main() { import { Foo } from './my_module'; const x = new Foo(); x.foo(); }