静态方法 Reflect.has() 作用与 in 操作符 相同。
句法
Reflect.has(target, propertyKey)
参数
-
target - 目标对象.
-
propertyKey - 属性名,需要检查目标对象是否存在此属性。
返回值
一个 Boolean 类型的对象指示是否存在此属性。
异常
如果目标对象并非TypeError。
描述
Reflect.has 用于检查一个对象是否拥有某个属性, 相当于in 操作符 。
示例
使用 Reflect.has()
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 | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) Reflect.has |
Standard | Initial definition. |
| ECMAScript Latest Draft (ECMA-262) Reflect.has |
Draft |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains.
Find out how you can help!
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 49 | 42 (42) | 未实现 | 未实现 | 未实现 |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | 未实现 | 未实现 | 42.0 (42) | 未实现 | 未实现 | 未实现 |