构造函数

constructor 属性用于指示被绑定的函数实际上应该转换为调用 JavaScript 中的 new 运算符。最后一个参数必须是从 JavaScript 导入的类型,它将在生成的粘合代码中使用。

#![allow(unused)]
fn main() {
#[wasm_bindgen]
extern "C" {
    type Shoes;

    #[wasm_bindgen(constructor)]
    fn new() -> Shoes;
}
}

这将为 Shoes 类型附加一个 new 静态方法,并且在 JavaScript 中调用此方法时,它将等效于 new Shoes()

#![allow(unused)]
fn main() {
// Become a cobbler; construct `new Shoes()`
let shoes = Shoes::new();
}