js_name = blah
The js_name
attribute can be used to bind to a different function in
JavaScript than the identifier that's defined in Rust.
Most often, this is used to convert a camel-cased JavaScript identifier into a snake-cased Rust identifier:
Sometimes, it is used to bind to JavaScript identifiers that are not valid Rust
identifiers, in which case js_name = "some string"
is used instead of js_name = ident
:
However, you can also use js_name
to define multiple signatures for
polymorphic JavaScript functions:
All of these functions will call console.log
in JavaScript, but each
identifier will have only one signature in Rust.
Note that if you use js_name
when importing a type you'll also need to use the
js_class
attribute when defining methods on the type:
The js_name
attribute can also be used in situations where a JavaScript module uses
export default
. In this case, setting the js_name
attribute to "default" on the
type
declaration, and the js_class
attribute to "default" on any methods
on the exported object will generate the correct imports.
For example, a module that would be imported directly in JavaScript:
import Foo from "bar";
let f = new Foo();
Could be accessed using this definition in Rust: