2018-08-06 16:35:28 -07:00
|
|
|
# `js_namespace = blah`
|
|
|
|
|
|
|
|
This attribute indicates that the JavaScript type is accessed through the given
|
|
|
|
namespace. For example, the `WebAssembly.Module` APIs are all accessed through
|
|
|
|
the `WebAssembly` namespace. `js_namespace` can be applied to any import
|
|
|
|
(function or type) and whenever the generated JavaScript attempts to reference a
|
|
|
|
name (like a class or function name) it'll be accessed through this namespace.
|
|
|
|
|
|
|
|
```rust
|
|
|
|
#[wasm_bindgen]
|
2018-11-27 12:27:00 -08:00
|
|
|
extern "C" {
|
2018-08-06 16:35:28 -07:00
|
|
|
#[wasm_bindgen(js_namespace = console)]
|
|
|
|
fn log(s: &str);
|
2020-05-27 17:34:41 +02:00
|
|
|
|
|
|
|
type Foo;
|
|
|
|
#[wasm_bindgen(constructor, js_namespace = Bar)]
|
|
|
|
fn new() -> Foo;
|
2018-08-06 16:35:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
log("hello, console!");
|
2020-05-27 17:34:41 +02:00
|
|
|
Foo::new();
|
2018-08-06 16:35:28 -07:00
|
|
|
```
|
|
|
|
|
2020-05-27 17:34:41 +02:00
|
|
|
This is an example of how to bind namespaced items in Rust. The `log` and `Foo::new` functions will
|
|
|
|
be available in the Rust module and will be invoked as `console.log` and `new Bar.Foo` in
|
2018-08-06 16:35:28 -07:00
|
|
|
JavaScript.
|