跳过
当附加到 pub
结构体字段时,表示该字段不会暴露给 JavaScript,并且不会在 ES6 类中生成 getter 或 setter。
#![allow(unused)] fn main() { use wasm_bindgen::prelude::*; #[wasm_bindgen] pub struct Foo { pub bar: u32, #[wasm_bindgen(skip)] pub baz: u32, } #[wasm_bindgen] impl Foo { pub fn new() -> Self { Foo { bar: 1, baz: 2 } } } }
在这里,bar
字段在 JS 中既可读又可写,而 baz
字段在 JS 中将是 undefined
。
import('./pkg/').then(rust => {
let foo = rust.Foo.new();
// bar is accessible by getter
console.log(foo.bar);
// field marked with `skip` is undefined
console.log(foo.baz);
// you can shadow it
foo.baz = 45;
// so accessing by getter will return `45`
// but it won't affect real value in rust memory
console.log(foo.baz);
});