To read and write arbitrary properties from any untyped JavaScript value regardless if it is an instanceof
some JavaScript class or not, use the js_sys::Reflect
APIs. These APIs are bindings to the JavaScript builtin Reflect
object and its methods.
You might also benefit from using duck-typed interfaces instead of working with untyped values.
js_sys::Reflect::get
API documentation for js_sys::Reflect::get
.
A function that returns the value of a property.
let value = js_sys::Reflect::get(&target, &property_key)?;
let value = target[property_key];
js_sys::Reflect::set
API documentation for js_sys::Reflect::set
.
A function that assigns a value to a property. Returns a boolean that is true if the update was successful.
js_sys::Reflect::set(&target, &property_key, &value)?;
target[property_key] = value;
js_sys::Reflect::has
API documentation for js_sys::Reflect::has
.
The JavaScript in
operator as function. Returns a boolean indicating whether an own or inherited property exists on the target.
if js_sys::Reflect::has(&target, &property_key)? { // ... } else { // ... }
if (property_key in target) { // ... } else { // ... }
See the js_sys::Reflect
API documentation for the full listing of JavaScript value reflection and introspection capabilities.