handler.has() 方法可以看作是针对 in 操作的钩子.
var p = new Proxy(target, {
has: function(target, prop) {
}
});
下面是传递给 has 方法的参数. this is bound to the handler.
targetprophas 方法返回一个 boolean 属性的值.
handler.has 方法可以看作是针对 in 操作的钩子.
这个钩子可以拦截下面这些操作:
foo in proxyfoo in Object.create(proxy)with 检查: with(proxy) { (foo); }Reflect.has()如果违反了下面这些规则, proxy 将会抛出 TypeError:
下面的代码 hook 了 in 操作.
var p = new Proxy({}, {
has: function(target, prop) {
console.log('called: ' + prop);
return true;
}
});
console.log('a' in p); // "called: a"
// true
下面的代码违反了规则.
var obj = { a: 10 };
Object.preventExtensions(obj);
var p = new Proxy(obj, {
has: function(target, prop) {
return false;
}
});
'a' in p; // TypeError is thrown
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) [[HasProperty]] |
Standard | Initial definition. |
| ECMAScript Latest Draft (ECMA-262) [[HasProperty]] |
Draft |
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | ? | 18 (18) | ? | ? | ? |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | ? | ? | 18.0 (18) | ? | ? | ? |
Proxyhandlerin operatorReflect.has()