diff --git a/src/js.rs b/src/js.rs index 849c75a1..9ed038c1 100644 --- a/src/js.rs +++ b/src/js.rs @@ -1036,6 +1036,12 @@ extern "C" { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/getPrototypeOf #[wasm_bindgen(static_method_of = Reflect, js_name = getPrototypeOf, catch)] pub fn get_prototype_of(target: &JsValue) -> Result; + + /// The static Reflect.has() method works like the in operator as a function. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/has + #[wasm_bindgen(static_method_of = Reflect, catch)] + pub fn has(target: &JsValue, property_key: &JsValue) -> Result; } // Set diff --git a/tests/all/js_globals/Reflect.rs b/tests/all/js_globals/Reflect.rs index 5f676576..9e572877 100644 --- a/tests/all/js_globals/Reflect.rs +++ b/tests/all/js_globals/Reflect.rs @@ -387,4 +387,50 @@ fn get_prototype_of() { "#, ) .test() +} + +#[test] +fn has() { + project() + .file( + "src/lib.rs", + r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js; + + #[wasm_bindgen] + pub fn has(target: &JsValue, property_key: &JsValue) -> JsValue { + let result = js::Reflect::has(target, property_key); + let result = match result { + Ok(val) => val, + Err(_err) => "TypeError".into() + }; + result + } + "#, + ) + .file( + "test.ts", + r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const object = { + property: 42 + }; + const array: number[] = [1, 2, 3, 4] + + assert.equal(wasm.has(object, "property"), true); + assert.equal(wasm.has(object, "foo"), false); + assert.equal(wasm.has(array, 3), true); + assert.equal(wasm.has(array, 10), false); + assert.equal(wasm.has("", "property"), "TypeError"); + } + "#, + ) + .test() } \ No newline at end of file