静态
Reflect
.has()
method works like the
in
operator
as a function.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Reflect.has(target, propertyKey)
target
The target object in which to look for the property.
propertyKey
The name of the property to check.
A
布尔
indicating whether or not the
target
has the property.
A
TypeError
, if
target
is not an
Object
.
Reflect.has
method allows you to check if a property is in an object. It works like the
in
operator
as a function.
Reflect.has()
Reflect.has({x: 0}, 'x') // true
Reflect.has({x: 0}, 'y') // false
// returns true for properties in the prototype chain
Reflect.has({x: 0}, 'toString')
// Proxy with .has() handler method
obj = new Proxy({}, {
has(t, k) { return k.startsWith('door') }
});
Reflect.has(obj, 'doorbell') // true
Reflect.has(obj, 'dormitory') // false
Reflect.has
返回
true
for any inherited properties, like the
in
operator
:
const a = {foo: 123}
const b = {__proto__: a}
const c = {__proto__: b}
// The prototype chain is: c -> b -> a
Reflect.has(c, 'foo') // true
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Reflect.has' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
has
|
Chrome 49 | Edge 12 | Firefox 42 | IE No | Opera 36 | Safari 10 | WebView Android 49 | Chrome Android 49 | Firefox Android 42 | Opera Android 36 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 6.0.0 |
完整支持
不支持
Reflect
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()