Reflect.has()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.

静态方法 Reflect.has() 作用与 in 操作符 相同。

句法

Reflect.has(target, propertyKey)

参数

target

目标对象。

propertyKey

属性名,需要检查目标对象是否存在此属性。

返回值

一个 Boolean 类型的对象指示是否存在此属性。

异常

如果目标对象并非Object 类型,抛出TypeError

描述

Reflect.has 用于检查一个对象是否拥有某个属性,相当于in 操作符

示例

使用 Reflect.has()

js
Reflect.has({ x: 0 }, "x"); // true
Reflect.has({ x: 0 }, "y"); // false

// 如果该属性存在于原型链中,返回 true
Reflect.has({ x: 0 }, "toString");

// Proxy 对象的 .has() 句柄方法
obj = new Proxy(
  {},
  {
    has(t, k) {
      return k.startsWith("door");
    },
  },
);
Reflect.has(obj, "doorbell"); // true
Reflect.has(obj, "dormitory"); // false

规范

Specification
ECMAScript® 2025 Language Specification
# sec-reflect.has

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
has

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

相关连接