{{JSRef("Global_Objects", "Proxy")}} {{harmony}}
Summary
A trap for {{jsxref("Operators/in", "in")}} operator.
Syntax
var proxy = new Proxy(target, { has: function(target, prop) { } });
Parameters
Following parameters are passed to has
method. this
is bound to the handler.
-
target
- The target object.
-
prop
- The name of the property to check existence.
Return value
has
method must return a boolean value.
Description
The handler.has
method is a trap for {{jsxref("Operators/in", "in")}} operator.
Invariants
- A property cannot be reported as non-existent, if it exists as a non-configurable own property of the target object.
- A property cannot be reported as non-existent, if it exists as an own property of the target object and the target object is not extensible.
Examples
Following code traps {{jsxref("Operators/in", "in")}} operator.
var proxy = new Proxy({}, { has: function(target, prop) { console.log("called: " + prop); return true; } }); console.log("a" in proxy); // "called: a" // true
Following code violates invariant.
var obj = { a: 10 }; Object.preventExtensions(obj); var proxy = new Proxy(obj, { has: function(target, prop) { return false; } }); "a" in proxy; // TypeError is thrown
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('ES6', '#sec-proxy-object-internal-methods-and-internal-slots-hasproperty-p', '[[HasProperty]]')}} | {{Spec2('ES6')}} | Initial definition. |
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatUnknown}} | {{CompatGeckoDesktop("18")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatGeckoMobile("18")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
See also
- {{jsxref("Proxy")}}
- {{jsxref("Proxy.handler", "handler")}}
- {{jsxref("Operators/in", "in")}} operator