constructor
constructor
屬性用於指示被綁定的函式實際上應轉換為在 JavaScript 中呼叫 new
運算子。最後一個參數必須是從 JavaScript 匯入的型別,並且它將在產生的 glue 中使用。
# #![allow(unused_variables)] #fn main() { #[wasm_bindgen] extern "C" { type Shoes; #[wasm_bindgen(constructor)] fn new() -> Shoes; } #}
這將附加一個 new
靜態方法到 Shoes
型別,並且在 JavaScript 中,當這個方法被呼叫時,它將等同於 new Shoes()
。
# #![allow(unused_variables)] #fn main() { // Become a cobbler; construct `new Shoes()` let shoes = Shoes::new(); #}