Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

constructor

The constructor attribute is used to indicate that the function being bound should actually translate to calling the new operator in JavaScript. The final argument must be a type that's imported from JavaScript, and it's what will get used in the generated glue:

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

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

This will attach a new static method to the Shoes type, and in JavaScript when this method is called, it will be equivalent to new Shoes().

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