handler.has()
method is a trap for the
in
operator.
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.
const p = new Proxy(target, {
has: function(target, prop) {
}
});
The following parameters are passed to
has()
方法。
this
is bound to the handler.
target
The target object.
prop
Symbol
of the property to check for existence.
has()
method must return a boolean value.
handler.has()
method is a trap for the
in
operator.
This trap can intercept these operations:
foo
in
proxy
foo in Object.create(
proxy
)
with
check:
with(
proxy
) { (
foo
); }
Reflect.has()
If the following invariants are violated, the proxy will throw a
TypeError
:
The following code traps the
in
operator.
const p = new Proxy({}, {
has: function(target, prop) {
console.log('called: ' + prop);
return true;
}
});
console.log('a' in p); // "called: a"
// true
The following code violates an invariant.
const obj = { a: 10 };
Object.preventExtensions(obj);
const p = new Proxy(obj, {
has: function(target, prop) {
return false;
}
});
'a' in p; // TypeError is thrown
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of '[[HasProperty]]' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
has
|
Chrome 49 | Edge 12 | Firefox 18 | IE No | Opera 36 | Safari 10 | WebView Android 49 | Chrome Android 49 | Firefox Android 18 | Opera Android 36 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 6.0.0 |
完整支持
不支持
Proxy
handler
in
operator
Reflect.has()