跳過
當附加到 pub
結構體欄位時,表示該欄位不會暴露給 JavaScript,也不會在 ES6 類別中產生 getter 或 setter。
# #![allow(unused_variables)] #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);
});