构造器
constructor
属性用于指示要绑定的函数实际上应该转换为在 JavaScript 中调用 new
操作符。最后一个参数必须是从 JavaScript 导入的类型,并且它将在生成的粘合代码中使用。
# #![allow(unused_variables)] #fn main() { #[wasm_bindgen] extern "C" { type Shoes; #[wasm_bindgen(constructor)] fn new() -> Shoes; } #}
这将为 Shoes
类型附加一个 new
静态方法,并且在 JavaScript 中调用此方法时,它等效于 new Shoes()
。
# #![allow(unused_variables)] #fn main() { // Become a cobbler; construct `new Shoes()` let shoes = Shoes::new(); #}