1# Accessing Properties of Untyped JavaScript Values 2 3To read and write arbitrary properties from any untyped JavaScript value 4regardless if it is an `instanceof` some JavaScript class or not, use [the 5`js_sys::Reflect` APIs][js-sys-reflect]. These APIs are bindings to the 6[JavaScript builtin `Reflect` object][mdn-reflect] and its methods. 7 8You might also benefit from [using duck-typed 9interfaces](./working-with-duck-typed-interfaces.html) instead of working with 10untyped values. 11 12## Reading Properties with `js_sys::Reflect::get` 13 14[API documentation for `js_sys::Reflect::get`.](https://docs.rs/js-sys/0.3.39/js_sys/Reflect/fn.get.html) 15 16A function that returns the value of a property. 17 18#### Rust Usage 19 20```rust 21let value = js_sys::Reflect::get(&target, &property_key)?; 22``` 23 24#### JavaScript Equivalent 25 26```js 27let value = target[property_key]; 28``` 29 30## Writing Properties with `js_sys::Reflect::set` 31 32[API documentation for `js_sys::Reflect::set`.](https://docs.rs/js-sys/0.3.39/js_sys/Reflect/fn.set.html) 33 34A function that assigns a value to a property. Returns a boolean that is true if 35the update was successful. 36 37#### Rust Usage 38 39```rust 40js_sys::Reflect::set(&target, &property_key, &value)?; 41``` 42 43#### JavaScript Equivalent 44 45```js 46target[property_key] = value; 47``` 48 49## Determining if a Property Exists with `js_sys::Reflect::has` 50 51[API documentation for `js_sys::Reflect::has`.](https://docs.rs/js-sys/0.3.39/js_sys/Reflect/fn.has.html) 52 53The JavaScript `in` operator as function. Returns a boolean indicating whether 54an own or inherited property exists on the target. 55 56#### Rust Usage 57 58```rust 59if js_sys::Reflect::has(&target, &property_key)? { 60 // ... 61} else { 62 // ... 63} 64``` 65 66#### JavaScript Equivalent 67 68```js 69if (property_key in target) { 70 // ... 71} else { 72 // ... 73} 74``` 75 76## But wait — there's more! 77 78See [the `js_sys::Reflect` API documentation][js-sys-reflect] for the full 79listing of JavaScript value reflection and introspection capabilities. 80 81[js-sys-reflect]: https://docs.rs/js-sys/latest/js_sys/Reflect/index.html 82[mdn-reflect]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect 83